更新的时候要将更新的model放到for循环里面new,否则上传多个文件时只会更新最后一条
/**
* 上传项目附件并更新附件表(多文件,之间用&隔开)
* @return fileName 文件名
* @return fileType 文件类型
* @return projectId 项目编号
*/
public Map<String, Object> prjUpload(HttpServletRequest request, String fileName, String projectId, boolean isUnique) throws IllegalStateException, IOException {
Map<String, Object> map = new HashMap<>();
PrjInfo prjInfo = new PrjInfo();
prjInfo = getPrjInfoByProjectId(projectId);
String projectName = prjInfo.getProjectName();
String[] fileNameArray = fileName.split("&");
//是否一个文件名对应一个文件类型
if(StringUtil.isEmpty(String.valueOf(fileNameArray))){
map.put("code", 418);
map.put("msg", "传入文件错误,请重新上传");
return map;
}else{
String filePath = "project-"+projectId;
//Map<String, Object> uploadResults = uploadListFiles(request, filePath, true);
//创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//判断 request 是否有文件上传,即多部分请求
StringBuffer buffer = new StringBuffer();
String localFileUrl="";
if (multipartResolver.isMultipart(request))
{
//转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
//取得request中的所有文件名 ***"files"为前台inpu框的name值***
List<MultipartFile> files = multiRequest.getFiles("files");
//MultipartFile类型的变量file在files集合范围内循环
for(MultipartFile file:files){
if (file != null)
{
//取得当前上传文件的文件名称
String myFileName = file.getOriginalFilename();
//如果名称不为"",说明该文件存在,否则说明该文件不存在
if (myFileName.trim() != ""){
//重命名上传后的文件名
String renameFileName = file.getOriginalFilename();
String absPath = WebConstants.SERVER_FILE_ESCAPE_PATH + filePath;
new File(absPath).mkdirs();
String path = absPath + File.separator + renameFileName;
if(isUnique){
String name = path.substring(0, path.lastIndexOf("."));
String suffix = path.substring(path.lastIndexOf("."), path.length());
path = name + suffix;
}
File localFile = new File(path);
file.transferTo(localFile);
localFileUrl=localFile.toString();
buffer.append(localFileUrl).append("&");
}
}
}
}
String localFileUrls = buffer.toString();
if (localFileUrls.endsWith("&")){
localFileUrls = localFileUrls.substring(0, localFileUrls.length() - 1);
}
for(int i=0; i < fileNameArray.length; i++){
PrjProgramUrl prjProgramUrl = new PrjProgramUrl();
String[] localFileUrlStrArray = localFileUrls.split("&");
Date createDate = DateUtil.getCurrentDate();
Integer createuserId = HttpUtils.getCurrentUser().getId();
String programId = projectId+"-" + fileNameArray[i];
String url = localFileUrlStrArray[i];
if (!StringUtil.isEmpty(url) && !StringUtil.isEmpty(fileNameArray[i])){
prjProgramUrl.setCreateDate(createDate);
prjProgramUrl.setCreateuserId(createuserId);
prjProgramUrl.setProgramId(programId);
prjProgramUrl.setUrl(url);
prjProgramUrlService.add(prjProgramUrl);
}
}
map.put("code", 200);
map.put("msg", "上传成功");
map.put("prjProgramUrl", null);
return map;
}
}
下载的时候不能用ajax下载,因为下载的文件会存到js的内存中,success 的data里面。但是不会下载,可以用window.open(url);
/**
* 文件的下载
*
* @param response
* 响应流
* @param filePath
* 文件的全路径
* @param fileName
* 输出流的文件名称
* @throws Exception
*/
public static void downloadFile(HttpServletResponse response, String filePath, String fileName) throws Exception {
File file = new File(filePath);
long fileLength = file.length();
response.setCharacterEncoding("utf-8");
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
InputStream inputStream = new FileInputStream(file);
OutputStream os = response.getOutputStream();
byte[] b = new byte[1024];
int length;
while ((length = inputStream.read(b)) > 0)
{
os.write(b, 0, length);
}
inputStream.close();
}