Commit 470d28f3 authored by frank.xa.zhang's avatar frank.xa.zhang

add attachment file upload function -- frank

parent a2da757d
...@@ -8,7 +8,6 @@ package pwc.taxtech.atms.common.util; ...@@ -8,7 +8,6 @@ package pwc.taxtech.atms.common.util;
* Version 1.0 * Version 1.0
**/ **/
import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -131,15 +130,17 @@ public class FileExcelUtil { ...@@ -131,15 +130,17 @@ public class FileExcelUtil {
public static void setFileHeadInfo(HttpServletResponse response, HttpServletRequest request, String fileName) { public static void setFileHeadInfo(HttpServletResponse response, HttpServletRequest request, String fileName) {
try { try {
// 获取客户端浏览器的类型 // // 获取客户端浏览器的类型
String agent = request.getHeader("User-Agent"); String agent = request.getHeader("User-Agent");
// 对文件名重新编码 // 对文件名重新编码
String encodingFileName = FileExcelUtil.encodeDownloadFilename(fileName, agent); String encodingFileName = FileExcelUtil.encodeDownloadFilename(fileName, agent);
// 告诉客户端允许断点续传多线程连接下载 // // 告诉客户端允许断点续传多线程连接下载
response.setHeader("Accept-Ranges", "bytes"); response.setHeader("Accept-Ranges", "bytes");
//文件后缀 //文件后缀
response.setContentType("application/force-download"); response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + encodingFileName); response.setHeader("Access-Control-Expose-Headers", "x-file-name");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.setHeader("x-file-name", encodingFileName);
} catch (IOException e) { } catch (IOException e) {
logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + "发生的异常是: ", e); logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + "发生的异常是: ", e);
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -208,15 +209,29 @@ public class FileExcelUtil { ...@@ -208,15 +209,29 @@ public class FileExcelUtil {
public static void downloadFile(HttpServletRequest request, HttpServletResponse response, String fileName, String filePathToBeServed) { public static void downloadFile(HttpServletRequest request, HttpServletResponse response, String fileName, String filePathToBeServed) {
FileExcelUtil.setFileHeadInfo(response, request, fileName); FileExcelUtil.setFileHeadInfo(response, request, fileName);
ServletOutputStream out = null;
try { try {
File fileToDownload = new File(filePathToBeServed); File fileToDownload = new File(filePathToBeServed);
InputStream inputStream = new FileInputStream(fileToDownload); InputStream inputStream = new FileInputStream(fileToDownload);
IOUtils.copy(inputStream, response.getOutputStream()); out = response.getOutputStream();
response.flushBuffer(); int b = 0;
byte[] buffer = new byte[512];
while ((b = inputStream.read(buffer)) > 0) {
out.write(buffer, 0, b);
}
inputStream.close(); inputStream.close();
} catch (Exception exception) { } catch (Exception exception) {
logger.error(exception.getMessage()); logger.error(exception.getMessage());
} }
finally {
try {
out.close();
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
......
...@@ -161,9 +161,9 @@ public class OrganizationHKController { ...@@ -161,9 +161,9 @@ public class OrganizationHKController {
@RequestMapping(value = "downloadAttachment", method = RequestMethod.GET) @RequestMapping(value = "downloadAttachment", method = RequestMethod.GET)
public @ResponseBody public @ResponseBody
OperationResultDto<Object> downloadAttachment(HttpServletRequest request, HttpServletResponse response, @RequestParam("attachmentId") Long attachmentId) { void downloadAttachment(HttpServletRequest request, HttpServletResponse response, @RequestParam("attachmentId") Long attachmentId) {
logger.info("POST /api/v1/orgHK/downloadAttachment"); logger.info("POST /api/v1/orgHK/downloadAttachment");
return organizationHKService.downloadAttachment(request, response, attachmentId); organizationHKService.downloadAttachment(request, response, attachmentId);
} }
@RequestMapping(value = "deleteAttachment", method = RequestMethod.DELETE) @RequestMapping(value = "deleteAttachment", method = RequestMethod.DELETE)
......
...@@ -606,15 +606,12 @@ public class OrganizationHKServiceImpl { ...@@ -606,15 +606,12 @@ public class OrganizationHKServiceImpl {
return saveResult; return saveResult;
} }
public OperationResultDto<Object> downloadAttachment(HttpServletRequest request, HttpServletResponse response, Long attachmentId) { public void downloadAttachment(HttpServletRequest request, HttpServletResponse response, Long attachmentId) {
OperationResultDto<Object> resultDto = new OperationResultDto<>();
OrganizationAttachment attachment = organizationAttachmentMapper.selectByPrimaryKey(attachmentId); OrganizationAttachment attachment = organizationAttachmentMapper.selectByPrimaryKey(attachmentId);
Assert.notNull(attachment, "attachemtn is null"); Assert.notNull(attachment, "attachemtn is null");
String fileName = attachment.getDocumentName(); String fileName = attachment.getDocumentName();
String filePath = fileDirectory + attachment.getDocumentPath(); String filePath = fileDirectory +"/"+ attachment.getDocumentPath();
FileExcelUtil.downloadFile(request, response, fileName, filePath); FileExcelUtil.downloadFile(request, response, fileName, filePath);
resultDto.setResult(true);
return resultDto;
} }
public OperationResultDto<Object> deleteAttachment(Long attachmentId) { public OperationResultDto<Object> deleteAttachment(Long attachmentId) {
......
infrastructureModule infrastructureModule
.controller('OrganizationManageController', ['$scope', '$location', '$log', 'orgHKService', '$interval', 'uiGridTreeViewConstants', 'uiGridConstants', 'SweetAlert', '$translate', '$timeout', 'apiInterceptor', '$q', 'loginContext', '$cookies', 'Upload', .controller('OrganizationManageController', ['$scope', '$location', '$log', 'orgHKService', '$interval', 'uiGridTreeViewConstants', 'uiGridConstants', 'SweetAlert', '$translate', '$timeout', 'apiInterceptor', '$q', 'loginContext', '$cookies', 'Upload','FileSaver',
function ($scope, $location, $log, orgHKService, $interval, uiGridTreeViewConstants, uiGridConstants, SweetAlert, $translate, $timeout, apiInterceptor, $q, loginContext, $cookies, Upload) { function ($scope, $location, $log, orgHKService, $interval, uiGridTreeViewConstants, uiGridConstants, SweetAlert, $translate, $timeout, apiInterceptor, $q, loginContext, $cookies, Upload,FileSaver) {
'use strict'; 'use strict';
$scope.expanded = false; $scope.expanded = false;
...@@ -1180,20 +1180,31 @@ ...@@ -1180,20 +1180,31 @@
window.downloadDocument = function (id) { window.downloadDocument = function (id) {
console.log("download document enter..."); console.log("download document enter...");
orgHKService.attachmentDownload(id).success(function (data, status, headers) { orgHKService.attachmentDownload(id).success(function (data, status, headers) {
var octetStreamMime = 'application/octet-stream'; var octetStreamMime = 'application/octet-stream;charset=UTF-8';
var contentType = headers('content-type') || octetStreamMime; //var contentType = headers('content-type') || octetStreamMime;
var blob = new Blob([data], { var blob = new Blob([data], {type: octetStreamMime});
type: octetStreamMime var attach = _.find($scope.attachmentDatasource, {id: id});
}); FileSaver.saveAs(blob, attach.documentName);
var attach = _.find($scope.attachmentDatasource,{id:id}); // if (window.navigator.msSaveBlob) {
if (window.navigator.msSaveBlob) { // navigator.msSaveBlob(blob, attach.documentName);
navigator.msSaveBlob(blob, "AdjustmentTable"); // } else {
} else { // // Try using other saveBlob implementations, if available
// Try using other saveBlob implementations, if available // var saveBlob = navigator.webkitSaveBlob || navigator.mozSaveBlob || navigator.saveBlob;
var saveBlob = navigator.webkitSaveBlob || navigator.mozSaveBlob || navigator.saveBlob; // if (saveBlob === undefined) {
if (saveBlob === undefined) throw "Not supported"; // var urlCreator = window.URL || window.webkitURL || window.mozURL || window.msURL;
saveBlob(blob, attach.documentName); // if (urlCreator) {
} // var a = document.createElement('a');
// a.href = urlCreator.createObjectURL(blob);
// a.target = '_blank';
// a.download = attach.documentName;
// document.body.appendChild(a);
// a.click();
// }
// }
// else {
// saveBlob(blob, attach.documentName);
// }
// }
console.log("download document finish..."); console.log("download document finish...");
}).error(function () { }).error(function () {
SweetAlert.error($translate.instant('PleaseContactAdministrator')); SweetAlert.error($translate.instant('PleaseContactAdministrator'));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment