//下载
@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;
}
@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;
}