您的位置:首页 > 编程语言 > Java开发

Java 文件的上传下载

2013-12-09 09:29 225 查看
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

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: