Java - byte[]流實現上傳下載(上傳byte[]至關係型數據庫,下載byte[]流,返回下載鏈接(ResponseEntity<byte[]>)對象)
- 返回 ResponseEntity<byte[]> 實現下載
public ResponseEntity<byte[]> createReportTwo(
//返回 ResponseEntity<byte[]>
HttpServletRequest request,
@ApiParam(required = true, name = "token", value = "token") @RequestParam(name = "token", required = true) String token,
@ApiParam(required = false, name = "orgId", value = "機構id") @RequestParam(name = "orgId", required = true) String orgId,
@ApiParam(required = true, name = "startTime", value = "開始時間") @RequestParam(name = "startTime", required = true) String startTime,
@ApiParam(required = true, name = "endTime", value = "結束時間") @RequestParam(name = "endTime", required = true) String endTime
){
try {
String address = this.resultStatisticalServiceImpl.createReport( token,orgId, startTime, endTime );
Map<String, Object> fileDetal = this.resultStatisticalServiceImpl.getFileDetal( address );
//fileDetal.get( "buffer" ) byte[]
//fileDetal.get( "fileName" ) 文件名
if (null != fileDetal && fileDetal.containsKey( "buffer" ) && null != fileDetal.get( "buffer" )){
HttpHeaders headers = new HttpHeaders();
String fileName = encodeFileName(request, fileDetal.get( "fileName" ).toString());
//attachment 附件屬性
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType( MediaType.APPLICATION_OCTET_STREAM);
//(byte[]) fileDetal.get( "buffer" ) byte[]流 對接
//HttpHeaders 對象
//HttpStatus.OK 狀態
return new ResponseEntity<byte[]>((byte[]) fileDetal.get( "buffer" ), headers, HttpStatus.OK);
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
- 實體類 (mysql中的類型 data = > longblob)
package com.file.domain;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "tableName")
public class AttachmentFile implements Serializable {
/**
* serialVersionUID
*/
private static final long serialVersionUID = -7262697489461991370L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Lob
private byte[] data; //@Lob 註解很重要
@OneToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "task_attachment_id")
private TaskAttachment attachment;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/**
* 獲得data
*
* @return data data
*/
public byte[] getData() {
return data;
}
/**
* 設定data
*
* @param data data
*/
public void setData(byte[] data) {
this.data = data;
}
/**
* 獲得attachment
*
* @return attachment attachment
*/
public TaskAttachment getAttachment() {
return attachment;
}
/**
* 設定attachment
*
* @param attachment attachment
*/
public void setAttachment(TaskAttachment attachment) {
this.attachment = attachment;
}
}
- upLoad
@Test
public void upLoadTests() throws IOException {
File file = new File("C:\\Users\\Chyb\\Desktop\\data.log");
MultipartFile multipartFile = null;//傳值,可以直接獲得 MultipartFile 類型數據
try {
FileInputStream input = new FileInputStream(file);
multipartFile = new MockMultipartFile("file", file.getName(), "log", IOUtils.toByteArray(input));
}catch (Exception e){
e.printStackTrace();
}
System.out.println(multipartFile.getOriginalFilename());
TaskAttachmentFile taf = new TaskAttachmentFile();
taf.setData(multipartFile.getBytes());
TaskAttachmentFile save = this.taskAttachmentFileDao.save( taf );
System.out.println(save.getId());
}
- 上傳完成 (data` longblob)
- 下載
@Test
public void downLoadTests() throws IOException {
Optional<TaskAttachmentFile> byId = this.taskAttachmentFileDao.findById( 2825L );
TaskAttachmentFile attachmentFile = byId.get();
DownloadVo vo = new DownloadVo();
vo.setData( attachmentFile.getData() );
HttpHeaders headers = new HttpHeaders();
String fileName = "newData.log";
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType( MediaType.APPLICATION_OCTET_STREAM);
// 返回下載文件 , 返回類型 Object
//@GetMapping(value = "/", produces = MediaType.TEXT_HTML_VALUE)
//return ResponseEntity<byte[]>(vo.getData(), headers, HttpStatus.OK);//在Controller 這樣返回,類型 Objcet
System.out.println("DATA : "+vo.getData() + ">" + headers +">"+ HttpStatus.OK);//這裏是測試類, 只做輸出流打印
}
- 下載實現 (swagger-ui 測試效果)
隨筆記錄,方便自己學習
2019-06-19