uploadAction
package com.power.web.action.kabp;
import java.io.File;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.power.util.FileOptUtil;
import com.power.web.action.base.AbstractBaseAction;
public class UploadAction extends AbstractBaseAction{
private static final long serialVersionUID = 1L;
private static final int BUFFER_SIZE = 16 * 1024;
// 文件标题
private String title;
// 上传文件域对象
private File userfile;
// 上传文件名
private String userfileFileName;
// 上传文件类型
private String userfileContentType;
// 保存文件的目录路径(通过依赖注入)
private String savePath;
public String fileUpload(){
//根据服务器的文件保存地址和原文件名创建目录文件全路径
String dstPath = request.getSession().getServletContext().getRealPath("");
try {
userfileFileName = FileOptUtil.createFileName(userfileFileName);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
dstPath += "/uploadFiles/" + userfileFileName;
/*System.out.println("上传的文件的类型:"+userfileFileName);
System.out.println("上传的文件的类型:"+ this.getUserfileContentType());*/
File dstFile = new File(dstPath);
copy(this.userfile, dstFile);
try {
response.getWriter().print("{success:true,filename:'"+userfileFileName+"'}");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void copy(File src, File dst) {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public File getUserfile() {
return userfile;
}
public void setUserfile(File userfile) {
this.userfile = userfile;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUserfileFileName() {
return userfileFileName;
}
public void setUserfileFileName(String userfileFileName) {
this.userfileFileName = userfileFileName;
}
public String getUserfileContentType() {
return userfileContentType;
}
public void setUserfileContentType(String userfileContentType) {
this.userfileContentType = userfileContentType;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
downAction
/**
* 下载文件
* @return
*/
public String downloadFile() {
response.setContentType(CONTENT_TYPE);
// 得到下载文件的名字
String filename = request.getParameter("filename");
try {
String basePath = request.getSession().getServletContext().getRealPath("");
// 解决中文乱码问题
// filename = new String(request.getParameter("filename").getBytes("iso-8859-1"), "gbk");
// 创建file对象
File file = new File(basePath +"M/uploadFiles/"+ filename);
// 设置response的编码方式
response.setContentType("application/x-msdownload");
// 写明要下载的文件的大小
response.setContentLength((int) file.length());
// 设置附加文件名
// response.setHeader("Content-Disposition","attachment;filename="+filename);
// 解决中文乱码
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes("gbk"), "iso-8859-1"));
// 读出文件到i/o流
FileInputStream fis = new FileInputStream(file);
BufferedInputStream buff = new BufferedInputStream(fis);
byte[] b = new byte[1024];// 相当于我们的缓存
long k = 0;// 该值用于计算当前实际下载了多少字节
// 从response对象中得到输出流,准备下载
OutputStream myout = response.getOutputStream();
// 开始循环下载
while (k < file.length()) {
int j = buff.read(b, 0, 1024);
k += j;
// 将b中的数据写到客户端的内存
myout.write(b, 0, j);
}
// 将写入到客户端的内存的数据,刷新到磁盘
myout.flush();
} catch (UnsupportedEncodingException e) {
// e.printStackTrace();
} catch (FileNotFoundException e) {
// e.printStackTrace();
} catch (IOException e) {
// e.printStackTrace();
}
return null;
}