spring mvc 上传下载

//下载
@RequestMapping("/attachmentDownload")
@ResponseBody
public ResponseEntity<byte[]> attachmentDownload(HttpServletRequest request, HttpServletResponse response,
HttpSession session, String attachmentId) {
logger.info("下载附件信息");
Map<String, Object> params = new HashMap<String, Object>();
// 附件id
AttachmentEntity attachmentEntity = attachmentService.selectByID(attachmentId);
// 附件名称
String attachmentName = attachmentEntity.getAttachmentName();
// 附件路径
String attachmentPath = attachmentEntity.getAttachmentUuid();
String filePath = "";
....................................
File sourcefile = new File(filePath + File.separator + attachmentPath);
try {
if (sourcefile.exists()) {
HttpHeaders headers = new HttpHeaders();
String fileName = new String(attachmentName.getBytes("UTF-8"), "iso-8859-1");
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(sourcefile), headers,
HttpStatus.CREATED);
}
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("附件下载异常!" + e.getMessage());
e.printStackTrace();
}
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}


//预览
@RequestMapping("/attachmentView")
@ResponseBody
public void attachmentView(HttpServletRequest request, HttpServletResponse response, HttpSession session,
String attachmentId) {
logger.info("文件预览");
response.setCharacterEncoding("gbk");
// 查询附件信息
AttachmentEntity attachmentEntity = attachmentService.selectByID(attachmentId);
// 文件路径
String filePath =
PropertiesUtil.getRootPath(DataUtil.FILEPATH) + File.separator + getUserCompanyNo(request) + File.separator
+ attachmentEntity.getLoanContractNo();
String fileTruePath = filePath + File.separator + attachmentEntity.getAttachmentUuid();
if (new File(fileTruePath).exists()) {
FileInputStream fis = null;
OutputStream os = null;
try {
fis = new FileInputStream(fileTruePath);
os = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024 * 8];
while ((count = fis.read(buffer)) != -1) {
os.write(buffer, 0, count);
os.flush();
}
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
} finally {
try {
fis.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


private void writeFile(MultipartFile files, AttachmentEntity attachment, JSONObject json, String araNo)
throws Exception {
// TODO Auto-generated method stub
logger.info("写入文件信息");
String filePath =
PropertiesUtil.getRootPath(DataUtil.FILEPATH) + File.separator + araNo + File.separator
+ attachment.getLoanContractNo();
String originalFilename = files.getOriginalFilename();
File rootFile = new File(filePath);
String suffix = files.getOriginalFilename().substring(files.getOriginalFilename().lastIndexOf("."));
String fileName = CommonUtils.getUuid() + suffix;
File targetFile = new File(filePath + File.separator + fileName);
if (!rootFile.exists()) {
rootFile.mkdirs();
}
files.transferTo(targetFile);
// 处理附件信息
attachementSave(attachment, files, fileName);
}

/异常捕获/
@ExceptionHandler
@ResponseBody
public JSONObject hanlerException(Exception ex, HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setContentType("text/html;charset=utf-8");
JSONObject json = new JSONObject();
json.put("status", "0");
json.put("message", "操作成功!");
if (ex instanceof MaxUploadSizeExceededException) {
long maxSize = ((MaxUploadSizeExceededException)ex).getMaxUploadSize();
json.put("message", "上传文件太大,不能超过" + maxSize / 1024 / 1024 + "M");
json.put("status", "1");
} else {
json.put("message", "上传失败");
json.put("status", "1");
}

return json;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值