上传与下载文件

@LogAnnotation
@RequestMapping(value = "/uploadIssuesList")
@ApiOperation(httpMethod = "POST", value = "上传文件")
public Wrapper> uploadIssuesList(HttpServletRequest request, @RequestParam("file") MultipartFile file)
throws Exception {
// 如果文件不为空,写入上传路径
if (!file.isEmpty()) {
// 上传文件路径
String OutputPath = path + "poi" + File.separatorChar + "xlsinput" + File.separatorChar + "problem";
Map map=new HashMap();
// 如果目录不存在则创建
File uploadDir = new File(OutputPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
// 上传文件名
String filename = file.getOriginalFilename();

//获取文件后缀
String nameSuffix=filename.substring(filename.lastIndexOf("."));
// if (!filename.matches("^.+\\.(?i)(xls)$") &&
// !filename.matches("^.+\\.(?i)(xlsx)$")) {
// throw new BusinessException("上传文件格式不正确");
// }
orderCode=request.getParameter("orderCode");
filename=orderCode +nameSuffix;
File filepath = new File(path, filename);
// 判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
// 将上传文件保存到一个目标文件当中
FileUtils.copyInputStreamToFile(file.getInputStream(),new File(OutputPath + File.separatorChar + filename));
url =OutputPath + File.separatorChar + filename;
map.put("url",OutputPath + File.separatorChar + filename);
map.put("filename",filename);
return WrapMapper.ok(map);
} else {
return WrapMapper.error("上传文件不能为空");
}
}


@Encrypt
@Decrypt
@PostMapping(value = "/copyFile")
@ApiOperation(httpMethod = "POST", value = "")
public Wrapper copyFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String sourFile = request.getParameter("sourFile");//目标文件
String destFile = request.getParameter("destFile");//复制到存放的目标位置
File file = new File(sourFile);
if(file.isFile()) {
String fileName =file.getName();
String outputName =destFile + fileName;
if(destFile == null) outputName = path +fileName;
FileInputStream input =new FileInputStream(sourFile);
FileOutputStream output =new FileOutputStream(outputName);
int in = input.read();
while( in != -1) {
output.write(in);
in =input.read();
}
output.flush();
output.close();
input.close();
return WrapMapper.ok();
} else {
return WrapMapper.error("下载文件不存在");
}
}

你可能感兴趣的:(上传与下载文件)