Java 文件的上传下载

package avicit.xacstd.module.stdedit.smfilelawinfo.action;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLEncoder;


import avicit.xacstd.module.stdedit.smfilelawinfo.domain.SmFileInforLawService;
import com.bstek.dorado.utils.DoradoException;
import com.cape.platform.framework.spring.SpringFactory;
import com.cape.platform.framework.view.base.ActionBase;


下载action :


public class SmFileLawDownloadAction extends ActionBase {


SmFileInforLawService smFileInforLawService = (SmFileInforLawService) SpringFactory
.getBean("smFileInforLawService");


/**
* 文件下载
*/
public void lawDownLoad() throws UnsupportedEncodingException {


HttpServletResponse response = this.getResponse();
HttpServletRequest request = this.getRequest();


String id = request.getParameter("id");
String fileName = smFileInforLawService.getColumnValuebyId(id,
"FILE_PATH");
String filePath = smFileInforLawService.getAttachFilePath();
String fileDir = filePath + File.separator + fileName;


response.reset();     // 清除response中的缓存信息
response.setCharacterEncoding("UTF-8");
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=\""
+ URLEncoder.encode(fileName, "UTF-8") + "\"");
FileInputStream fs = null;
try {
File file = new File(fileDir);
fs = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
//写出流信息
byte[] content = new byte[1024];
int length;
while ((length = fs.read(content)) != -1) {
out.write(content, 0, length);
}
out.flush();
out.close();
} catch (FileNotFoundException e) {
new DoradoException("系统找不见文件!");
} catch (IOException e) {
new DoradoException("文件下载失败!");
}
}


}

上传action :


package avicit.xacstd.module.stdedit.smfilelawinfo.action;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import avicit.xacstd.module.stdedit.smfilelawinfo.domain.SmFileInforLawService;
import com.bstek.dorado.action.Action;
import com.bstek.dorado.action.mapping.ActionForward;
import com.bstek.dorado.biz.FileController;
import com.bstek.dorado.utils.StringHelper;
import com.cape.platform.framework.exception.DaoException;
import com.cape.platform.framework.spring.SpringFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class SmFileLawUploadController extends FileController{


private static Log logger = LogFactory.getLog(SmFileLawUploadController.class);
SmFileInforLawService smFileInforLawService = (SmFileInforLawService)SpringFactory.getBean("smFileInforLawService");

public ActionForward doUpload(Action action, HttpServletRequest request,HttpServletResponse response) throws Exception{


//获取记录的ID
String recordId = request.getParameter("recordId");

// 创建一个DiskFileItemFactory
DiskFileItemFactory factory = new DiskFileItemFactory();
// 创建一个ServletFileUpload
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置编码,解决上传中文名乱码问题
upload.setHeaderEncoding("utf-8");

String fileName = "";     
String uploadDir = "";   
String storePath = "";    
String storeFullName = "";
try{
// 取得要上传的文件。OaAttachment
List<FileItem> fileItems = upload.parseRequest(request);
int size = fileItems.size();
for (int i = 0; i < size; ++i) {
FileItem fi = (FileItem) fileItems.get(i);
if (!(fi.isFormField())) {

uploadDir = fi.getName();
if (StringHelper.isNotEmpty(uploadDir)) {
fileName =  uploadDir.substring(uploadDir.lastIndexOf("\\")+1, uploadDir.length());
   storePath = smFileInforLawService.getAttachFilePath();

try {
File file = new File(storePath);
if(!file.exists()){
file.mkdirs();
}
} catch (Exception e) { 
throw new DaoException("上传失败无法获得操作系统绝对地址!");
}
//文件名字格式  记录id_文件名.pdf
storeFullName = storePath + File.separator + fileName;
  
File storeFile = new File(storeFullName);
InputStream inputStream = fi.getInputStream();

byte[] bt = new byte[1024];
int count;
try {
FileOutputStream out = new FileOutputStream(storeFile);
while((count = inputStream.read(bt)) != -1){
try {
out.write(bt, 0, count);
} catch (IOException e) {
e.printStackTrace();
}
}
inputStream.close();
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}catch (FileUploadException e) {
logger.error("处理附件出错!", e);
throw new DaoException("文件上传出现错误!");
}
request.setAttribute("result", "sucess");
return action.findDefaultForward();
}


@Override
protected String getDownLoadFileName(HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}


@Override
protected InputStream getDownloadFileInputStream(HttpServletRequest arg0)
throws Exception {

// TODO Auto-generated method stub
return null;
}


@Override
protected void initFileUpload(HttpServletRequest arg0, DiskFileUpload fileUpload) {
// TODO Auto-generated method stub

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值