Commit 00227a49 by kangxiaoshan

上传附件

parent c3405769
......@@ -178,7 +178,7 @@
<redis.event.database>13</redis.event.database>
<!--Redis setting//end-->
<downhost>http://172.23.6.108:9001</downhost>
<ddb.accesskey>AKIAOS2UEXIFNVER2O6A</ddb.accesskey>
<ddb.secretkey>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</ddb.secretkey>
<ddb.region>cn-north-1</ddb.region>
......@@ -286,6 +286,7 @@
<redis.event.database>13</redis.event.database>
<!--Redis setting//end-->
<downhost>http://172.31.25.137:9001</downhost>
<ddb.accesskey>AKIA3XXNRTS2KXU632MD</ddb.accesskey>
<ddb.secretkey>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</ddb.secretkey>
<ddb.region>cn-north-1</ddb.region>
......
package common.controller;
import common.model.DmpIncome;
import common.model.PdAttachment;
import common.model.User;
import common.service.DmpIncomeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import security.annotation.CurrentAccount;
import util.ResultModel;
@Controller
@RequestMapping("{platform}")
public class AttachmnetUploadController {
@Autowired
DmpIncomeService dmpIncomeService;
//上传附件
@PostMapping("/upload/attachment")
@ResponseBody
public ResultModel uploadAttach(@RequestParam("file") MultipartFile[] files, String contractCode, @CurrentAccount User user) {
String erroNames = "";
for (MultipartFile file : files) {
PdAttachment attachment = dmpIncomeService.uploadPdAttach(file, contractCode, user);
if (attachment.getFaild()) {
erroNames += " " + attachment.getFileOrginName();
}
}
if (!StringUtils.isEmpty(erroNames)) {
return ResultModel.ERROR(5001, "文件:" + erroNames + "上传失败");
}
return ResultModel.OK();
}
//下载附加
@RequestMapping(value = "/download/attachment/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<byte[]> getDownloadResponseEntity(@PathVariable Long id) {
return dmpIncomeService.downloadAttach(id);
}
//删除附件
@PostMapping("/delete/attachment/{id}")
@ResponseBody
public ResultModel deleteAttach(@PathVariable Long id) {
return ResultModel.OK(dmpIncomeService.deleteAttach(id));
}
//附件列表
@GetMapping("/list/attachment")
@ResponseBody
public ResultModel attachList(String code,String startDate,String endDate, @PathVariable String platform) {
return dmpIncomeService.attachList(code,startDate,endDate, platform);
}
}
......@@ -48,22 +48,23 @@ public class PdController {
return ResultModel.OK(dmpIncomeService.pdDelete(income));
}
//上传附件
@PostMapping("/upload/attachment")
public ResultModel uploadAttach(@RequestParam("file") MultipartFile file, String contractCode, @CurrentAccount User user) {
////上传附件
//@PostMapping("/upload/attachment")
//public ResultModel uploadAttach(@RequestParam("file") MultipartFile file, String contractCode, @CurrentAccount User user) {
//
// return ResultModel.OK(dmpIncomeService.uploadPdAttach(file, contractCode, user));
//}
//
////下载附加
//@RequestMapping(value = "download/attachment/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
//public ResponseEntity<byte[]> getDownloadResponseEntity(@PathVariable Long id) {
// return dmpIncomeService.downloadAttach(id);
//}
//
////删除附件
//@PostMapping("/delete/load/attachment/{id}")
//public ResultModel deleteAttach(@PathVariable Long id) {
// return ResultModel.OK(dmpIncomeService.deleteAttach(id));
//}
return ResultModel.OK(dmpIncomeService.uploadPdAttach(file, contractCode, user));
}
//下载附加
@RequestMapping(value = "download/attachment/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<byte[]> getDownloadResponseEntity(@PathVariable Long id) {
return dmpIncomeService.downloadAttach(id);
}
//删除附件
@PostMapping("/delete/load/attachment/{id}")
public ResultModel deleteAttach(@PathVariable Long id) {
return ResultModel.OK(dmpIncomeService.deleteAttach(id));
}
}
......@@ -3,6 +3,7 @@ package common.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.util.Date;
@Entity
......@@ -13,7 +14,10 @@ public class PdAttachment {
private String fileName;//文件名
private String fileType;//文件类型
private Date uploadDate;//上传日期
private String uploadDateValue;
private String uploadUser;
private Boolean isFaild;// 是否失败
private String downloadUrl;
@Id
@GeneratedValue
......@@ -72,4 +76,30 @@ public class PdAttachment {
public void setFileType(String fileType) {
this.fileType = fileType;
}
@Transient
public Boolean getFaild() {
return isFaild;
}
public void setFaild(Boolean faild) {
isFaild = faild;
}
public String getUploadDateValue() {
return uploadDateValue;
}
public void setUploadDateValue(String uploadDateValue) {
this.uploadDateValue = uploadDateValue;
}
@Transient
public String getDownloadUrl() {
return downloadUrl;
}
public void setDownloadUrl(String downloadUrl) {
this.downloadUrl = downloadUrl;
}
}
......@@ -2,6 +2,14 @@ package common.repository;
import common.model.PdAttachment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface PdAttachmentRepository extends JpaRepository<PdAttachment, Long> {
List<PdAttachment> findBycontractCode(String code);
@Query(value = "select * from pd_attachment where contract_code=?1 and upload_date >= ?2 and upload_date <= ?3", nativeQuery = true)
List<PdAttachment> findBycontractCodeAndDate(String code, String startDate, String endDate);
}
......@@ -7,7 +7,6 @@ import common.model.User;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
import tkio.model.Account;
import util.ResultModel;
import java.util.List;
......@@ -46,4 +45,5 @@ public interface DmpIncomeService {
HSSFWorkbook exportPdIncomeList(String startDate, String endDate, String bodyCode, String serchName);
ResultModel attachList(String code, String startDate, String endDate, String platform);
}
......@@ -26,7 +26,7 @@ import util.ResultModel;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.MathContext;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
......@@ -70,7 +70,6 @@ public class DmpIncomeServiceImpl implements DmpIncomeService {
PdAttachmentRepository pdAttachmentRepository;
public static final Map<String, String> SHEET_NAMES = new HashMap();
private static final Map<String, String> CONTRACT_TYPE_NAME = new HashMap();
private static final Map<String, String> BUSINESS_TYPE_NAME = new HashMap();
......@@ -716,23 +715,23 @@ public class DmpIncomeServiceImpl implements DmpIncomeService {
@Override
public PdAttachment uploadPdAttach(MultipartFile file, String contractCode, User user) {
InputStream inputStream = null;
PdAttachment attachment = new PdAttachment();
try {
inputStream = file.getInputStream();
PdAttachment attachment = new PdAttachment();
attachment.setUploadDate(new Date());
attachment.setUploadDateValue(new DateTime(attachment.getUploadDate()).toString("yyyy-MM-dd HH:mm:ss"));
attachment.setUploadUser(user.getName());
attachment.setFileOrginName(file.getOriginalFilename());
attachment.setFileName(contractCode + "_" + file.getOriginalFilename());
attachment.setContractCode(contractCode);
attachment.setFileType(file.getContentType());
attachment.setFaild(false);
// 上传文件
String s3key = Constant.uploadPath + attachment.getFileName();
String s3key = Constant.uploadPath + "/" + attachment.getFileName();
String contentType = file.getContentType();
AwsS3Util.getInstance().uploadStreamToS3(Constant.bucket, s3key,
inputStream, contentType, file.getSize());
pdAttachmentRepository.save(attachment);
return attachment;
} catch (IOException e) {
......@@ -744,24 +743,27 @@ public class DmpIncomeServiceImpl implements DmpIncomeService {
}
}
}
return null;
attachment.setFaild(true);
return attachment;
}
@Override
public ResponseEntity<byte[]> downloadAttach(Long id) {
PdAttachment attachment = pdAttachmentRepository.findOne(id);
final String s3key = Constant.uploadPath + attachment.getFileName();
final String s3key = Constant.uploadPath + "/" + attachment.getFileName();
String fileName = attachment.getFileOrginName();
InputStream in = null;
byte[] content;
String fileType = attachment.getFileType();
try {
fileName = URLEncoder.encode(fileName, "UTF-8");
in = AwsS3Util.getInstance().downloadStreamFromS3(Constant.bucket, s3key);
content = IOUtils.toByteArray(in);
HttpHeaders headers = new HttpHeaders();
headers.set("filename", fileName);
headers.add("content-disposition", "attachment; filename=" + fileName);
headers.add("content-disposition", "attachment;filename=" + fileName);
headers.add("Content-Type", fileType);
return new ResponseEntity<>(content, headers, org.springframework.http.HttpStatus.CREATED);
} catch (Exception e) {
......@@ -781,4 +783,14 @@ public class DmpIncomeServiceImpl implements DmpIncomeService {
return attachment;
}
@Override
public ResultModel attachList(String code, String startDate, String endDate, String platform) {
List<PdAttachment> attachments;
if (StringUtils.isEmpty(startDate)) {
attachments = pdAttachmentRepository.findBycontractCodeAndDate(code,startDate,endDate);
} else {
attachments = pdAttachmentRepository.findBycontractCode(code);
}
return ResultModel.OK(attachments);
}
}
......@@ -45,6 +45,7 @@ public class Constant {
public static String ddbregion = ddbBundle.getString("ddb.region");
public static String bucket = ddbBundle.getString("ddb.bucket");
public static String uploadPath = ddbBundle.getString("ddb.upload.path");
public static String downhost = ddbBundle.getString("downhost");
}
......@@ -3,3 +3,5 @@ ddb.secretkey=${ddb.secretkey}
ddb.region=${ddb.region}
ddb.bucket=${ddb.bucket}
ddb.upload.path=${ddb.upload.path}
downhost=${downhost}
\ No newline at end of file
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