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;
* Version 1.0
**/
import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
......@@ -131,15 +130,17 @@ public class FileExcelUtil {
public static void setFileHeadInfo(HttpServletResponse response, HttpServletRequest request, String fileName) {
try {
// 获取客户端浏览器的类型
// // 获取客户端浏览器的类型
String agent = request.getHeader("User-Agent");
// 对文件名重新编码
String encodingFileName = FileExcelUtil.encodeDownloadFilename(fileName, agent);
// 告诉客户端允许断点续传多线程连接下载
// // 告诉客户端允许断点续传多线程连接下载
response.setHeader("Accept-Ranges", "bytes");
//文件后缀
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment; filename=" + encodingFileName);
response.setContentType("application/octet-stream;charset=UTF-8");
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) {
logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + "发生的异常是: ", e);
throw new RuntimeException(e);
......@@ -208,15 +209,29 @@ public class FileExcelUtil {
public static void downloadFile(HttpServletRequest request, HttpServletResponse response, String fileName, String filePathToBeServed) {
FileExcelUtil.setFileHeadInfo(response, request, fileName);
ServletOutputStream out = null;
try {
File fileToDownload = new File(filePathToBeServed);
InputStream inputStream = new FileInputStream(fileToDownload);
IOUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
out = response.getOutputStream();
int b = 0;
byte[] buffer = new byte[512];
while ((b = inputStream.read(buffer)) > 0) {
out.write(buffer, 0, b);
}
inputStream.close();
} catch (Exception exception) {
logger.error(exception.getMessage());
}
finally {
try {
out.close();
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
......
......@@ -161,9 +161,9 @@ public class OrganizationHKController {
@RequestMapping(value = "downloadAttachment", method = RequestMethod.GET)
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");
return organizationHKService.downloadAttachment(request, response, attachmentId);
organizationHKService.downloadAttachment(request, response, attachmentId);
}
@RequestMapping(value = "deleteAttachment", method = RequestMethod.DELETE)
......
......@@ -606,15 +606,12 @@ public class OrganizationHKServiceImpl {
return saveResult;
}
public OperationResultDto<Object> downloadAttachment(HttpServletRequest request, HttpServletResponse response, Long attachmentId) {
OperationResultDto<Object> resultDto = new OperationResultDto<>();
public void downloadAttachment(HttpServletRequest request, HttpServletResponse response, Long attachmentId) {
OrganizationAttachment attachment = organizationAttachmentMapper.selectByPrimaryKey(attachmentId);
Assert.notNull(attachment, "attachemtn is null");
String fileName = attachment.getDocumentName();
String filePath = fileDirectory + attachment.getDocumentPath();
String filePath = fileDirectory +"/"+ attachment.getDocumentPath();
FileExcelUtil.downloadFile(request, response, fileName, filePath);
resultDto.setResult(true);
return resultDto;
}
public OperationResultDto<Object> deleteAttachment(Long attachmentId) {
......
infrastructureModule
.controller('OrganizationManageController', ['$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) {
.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,FileSaver) {
'use strict';
$scope.expanded = false;
......@@ -1180,20 +1180,31 @@
window.downloadDocument = function (id) {
console.log("download document enter...");
orgHKService.attachmentDownload(id).success(function (data, status, headers) {
var octetStreamMime = 'application/octet-stream';
var contentType = headers('content-type') || octetStreamMime;
var blob = new Blob([data], {
type: octetStreamMime
});
var attach = _.find($scope.attachmentDatasource,{id:id});
if (window.navigator.msSaveBlob) {
navigator.msSaveBlob(blob, "AdjustmentTable");
} else {
// Try using other saveBlob implementations, if available
var saveBlob = navigator.webkitSaveBlob || navigator.mozSaveBlob || navigator.saveBlob;
if (saveBlob === undefined) throw "Not supported";
saveBlob(blob, attach.documentName);
}
var octetStreamMime = 'application/octet-stream;charset=UTF-8';
//var contentType = headers('content-type') || octetStreamMime;
var blob = new Blob([data], {type: octetStreamMime});
var attach = _.find($scope.attachmentDatasource, {id: id});
FileSaver.saveAs(blob, attach.documentName);
// if (window.navigator.msSaveBlob) {
// navigator.msSaveBlob(blob, attach.documentName);
// } else {
// // Try using other saveBlob implementations, if available
// var saveBlob = navigator.webkitSaveBlob || navigator.mozSaveBlob || navigator.saveBlob;
// if (saveBlob === undefined) {
// var urlCreator = window.URL || window.webkitURL || window.mozURL || window.msURL;
// 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...");
}).error(function () {
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