标签:
利用Struts进行图片上传<!-- 上传文件临时文件位置 -->
<constant name="struts.multipart.saveDir" value="/tmp"/>
<constant name="struts.multipart.maxSize" value="1000000000"/>
package edu.ING.ING_pro_school.school.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
public class SetFaceAction {
private static final long serialVersionUID =1L;
private static final int BUFFER_SIZE =16*1024;
// 页面图片
private File photoFile;
// 图片名
private String photoFileFileName;
// 图片类型
private String photoFileContentType;
private String img;
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public File getPhotoFile() {
return photoFile;
}
public void setPhotoFile(File photoFile) {
this.photoFile = photoFile;
}
public String getPhotoFileFileName() { return photoFileFileName;
}
public void setPhotoFileFileName(String photoFileFileName) {
this.photoFileFileName = photoFileFileName;
}
public String getPhotoFileContentType() {
return photoFileContentType;
}
public void setPhotoFileContentType(String photoFileContentType) {
this.photoFileContentType = photoFileContentType;
}
/*
* 取得运行时服务器目录
*
* @return 服务器根目录
*/
public String getSavePath() {
// 如果是从服务器上取就用这个获得系统的绝对路径方法。
return ServletActionContext.getServletContext().getRealPath("/");
}
public String download() throws UnsupportedEncodingException {
String path = getSavePath() +"\\UploadImages\\"+this.getImg(); System.out.println("path is "+ path);
BufferedInputStream bis =null;
BufferedOutputStream bos =null;
OutputStream fos =null;
InputStream fis =null;
String filepath = path;
System.out.println("文件路径"+ filepath);
File uploadFile =new File(filepath);
try {
fis =new FileInputStream(uploadFile);
bis =new BufferedInputStream(fis);
fos = ServletActionContext.getResponse().getOutputStream();
bos =new BufferedOutputStream(fos);
// 这个就就是弹出下载对话框的关键代码
ServletActionContext.getResponse().setHeader("Content-disposition",
"attachment;filename="+ URLEncoder.encode(path, "gbk"));
int bytesRead =0;
// 这个地方的同上传的一样。我就不多说了,都是用输入流进行先读,然后用输出流去写,唯一不同的是我用的是缓冲输入输出流
byte[] buffer =new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) !=-1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 上传图片
* 修改
* @return
* @throws Exception
*/
public String setFace() throws Exception {
if (null!=this.getPhotoFile()) {
// 从画面上取得图片文件
File files =this.getPhotoFile();
// 文件名 = 文件名 + 日期
photoFileFileName =new Date().getTime() + photoFileFileName.trim();
String savePath = getSavePath() +"\\UploadImages\\";
// 判断保存用文件夹是否存在
mkdir(savePath);
// 保存用的数据流生成
FileOutputStream fos =new FileOutputStream(savePath + photoFileFileName);
System.out.println(savePath + photoFileFileName);
// 保存文件
FileInputStream fis =new FileInputStream(files);
byte[] buffer =new byte[BUFFER_SIZE];
int len =0;
while ((len = fis.read(buffer)) >0) {
fos.write(buffer, 0, len);
}
}
return"";
}
/*
* 判断保存用文件夹是否存在
*Java中文网:http://www.javaweb.cc
* @param path 保存用的文件夹路径
*/
public void mkdir(String path) {
File file =new File(path);
// 如果存在
if (file.exists()) {
System.out.println("the dir is exits");
// 如果不存在,新建
} else {
file.mkdir();
System.out.println("have made a dir");
}
}
}版权声明:感觉我写的还算不错的的话希望你能够动动你的鼠标和键盘为我点上一个赞或是为我奉献上一个评论,在下感激不尽!_______________________________________________________欢迎转载,希望在你转载的同时,添加原文地址,谢谢配合
标签:
原文地址:http://blog.csdn.net/u011225629/article/details/48009653