java 文件上传和下载处理
2016-02-19 10:28
405 查看
Util.java:
文件上传下载处理:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.bio.framework.util.common.ImageUtil;
import com.bio.web.svc.model.SvcApply;
import com.bio.web.svc.model.SvcProject;
import com.bio.web.svc.model.SvcResultDocs;
import com.bio.web.svc.service.SvcApplyManager;
import com.bio.web.svc.service.SvcProjectManager;
import com.bio.web.svc.service.SvcResultDocsManager;
@Controller
@RequestMapping(value = "/backoffice")
public class UploadFileController {
@Autowired
private SvcResultDocsManager svcResultDocsManager;
@Autowired
private SvcApplyManager svcApplyManager;
@Autowired
private SvcProjectManager svcProjectManager;
@Value("#{applicationProperties[dataFiles]}")
private String dataFiles;
/**
* 文件上传
**/
@RequestMapping(value = "/UploadFile/uploadfileSave", method = RequestMethod.POST)
public void uploadfileSave(HttpServletRequest request, HttpServletResponse response) throws Exception {
Integer applyId = Integer.parseInt(request.getParameter("applyId"));
SvcApply svcapply = svcApplyManager.getById(applyId);
SvcProject svcProject = svcProjectManager.getById(svcapply.getProjectId());
//拼接路径
String path = dataFiles + "member-file" + File.separatorChar + svcapply.getMemberId() + File.separatorChar
+ "service-file" + File.separatorChar + "unitinfo-file" + File.separatorChar
+ svcProject.getUnitinfoId();
//判断是否存储路径,不存在则创建
ImageUtil.isExist(path);
String fileName = request.getParameter("Filename");
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile filedata = (MultipartFile) multipartRequest.getFile("Filedata");
if (filedata != null) {
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String myTime = format.format(date);
String typ = fileName.substring(fileName.lastIndexOf("."));
String pathA = path + File.separatorChar + myTime + typ;
File file = new File(pathA);
filedata.transferTo(file);
svcResultDocsManager.saveSvcResultDocs(applyId, fileName, pathA);
}
}
/**
* 文件下载
**/
@RequestMapping(value = "/UploadFile/downloadFile", method = RequestMethod.GET)
public void downloadFile(HttpServletRequest request, HttpServletResponse response, Integer fileId)
throws Exception {
SvcResultDocs svcResultDocs = svcResultDocsManager.getById(fileId);
// 获得请求文件名
String filepath = svcResultDocs.getFilepath();
String filename = URLEncoder.encode(svcResultDocs.getFilenm(), "UTF-8");
if (filename.length() > 150) { // 解决IE 6.0 bug
filename = new String(svcResultDocs.getFilenm().getBytes("GBK"), "ISO-8859-1");
}
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(filepath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final IOException e) {
System.out.println("IOException." + e);
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
}
public static void isExist(String path) { File file = new File(path); //判断文件夹是否存在,如果不存在则创建文件夹 if (!file.exists()){ file.mkdirs(); } }
文件上传下载处理:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.bio.framework.util.common.ImageUtil;
import com.bio.web.svc.model.SvcApply;
import com.bio.web.svc.model.SvcProject;
import com.bio.web.svc.model.SvcResultDocs;
import com.bio.web.svc.service.SvcApplyManager;
import com.bio.web.svc.service.SvcProjectManager;
import com.bio.web.svc.service.SvcResultDocsManager;
@Controller
@RequestMapping(value = "/backoffice")
public class UploadFileController {
@Autowired
private SvcResultDocsManager svcResultDocsManager;
@Autowired
private SvcApplyManager svcApplyManager;
@Autowired
private SvcProjectManager svcProjectManager;
@Value("#{applicationProperties[dataFiles]}")
private String dataFiles;
/**
* 文件上传
**/
@RequestMapping(value = "/UploadFile/uploadfileSave", method = RequestMethod.POST)
public void uploadfileSave(HttpServletRequest request, HttpServletResponse response) throws Exception {
Integer applyId = Integer.parseInt(request.getParameter("applyId"));
SvcApply svcapply = svcApplyManager.getById(applyId);
SvcProject svcProject = svcProjectManager.getById(svcapply.getProjectId());
//拼接路径
String path = dataFiles + "member-file" + File.separatorChar + svcapply.getMemberId() + File.separatorChar
+ "service-file" + File.separatorChar + "unitinfo-file" + File.separatorChar
+ svcProject.getUnitinfoId();
//判断是否存储路径,不存在则创建
ImageUtil.isExist(path);
String fileName = request.getParameter("Filename");
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile filedata = (MultipartFile) multipartRequest.getFile("Filedata");
if (filedata != null) {
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String myTime = format.format(date);
String typ = fileName.substring(fileName.lastIndexOf("."));
String pathA = path + File.separatorChar + myTime + typ;
File file = new File(pathA);
filedata.transferTo(file);
svcResultDocsManager.saveSvcResultDocs(applyId, fileName, pathA);
}
}
/**
* 文件下载
**/
@RequestMapping(value = "/UploadFile/downloadFile", method = RequestMethod.GET)
public void downloadFile(HttpServletRequest request, HttpServletResponse response, Integer fileId)
throws Exception {
SvcResultDocs svcResultDocs = svcResultDocsManager.getById(fileId);
// 获得请求文件名
String filepath = svcResultDocs.getFilepath();
String filename = URLEncoder.encode(svcResultDocs.getFilenm(), "UTF-8");
if (filename.length() > 150) { // 解决IE 6.0 bug
filename = new String(svcResultDocs.getFilenm().getBytes("GBK"), "ISO-8859-1");
}
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(filepath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final IOException e) {
System.out.println("IOException." + e);
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
}
<a href="${ctx}/backoffice/UploadFile/downloadFile.do?fileId=${item.id}"><spring:message code="common.label.download" text="下载"/></a>
相关文章推荐
- 验证码图片生成工具类——Captcha.java
- 文件相关操作工具类——FileUtils.java
- 自己动手写Java大整数《3》除法和十进制转换
- Java开发中的23种设计模式详解
- Spring MVC
- Java集合框架List,Map,Set等全面介绍
- java 随机生成身份证代码
- Java中&与&&;|与||的区别
- Eclipse的Git插件Egit: merge合并冲突具体解决方法
- 探知Java虚拟机——初识Java
- Type Java compiler level does not match the version of the installed Java project facet
- Java 5种字符串拼接方式性能比较。
- java中有用小函数收集
- Spring Bean的生命周期
- DUBBO Caused by: java.nio.channels.ClosedChannelException
- 深入分析JavaWeb 32 -- 数据库连接池
- 深入分析JavaWeb 31 -- JDBC(MySQL)事务管理
- 深入分析JavaWeb 30 -- 使用JDBC进行批处理
- SpringMvc多视图整合配置教程(jsp、velocity、freemarker)
- Eclipse中文乱码问题