Util.java:
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>