이 답변은 이미 위에 답변되었습니다. 최근 바이트 배열 객체를 multipartfile 객체로 변환하기위한 요구 사항을 처리하고 있습니다. 이 작업에는 두 가지 방법이 있습니다.
접근 1 : 당신이 그것을 만들 수 FileDiskItem 개체를 사용하는 경우
는 기본 CommonsMultipartFile를 사용합니다. 예 : 당신이 그것을 만들 수 FileDiskItem 개체를 사용하는 경우
Approach 1:
는 기본 CommonsMultipartFile를 사용합니다. 예 :
FileItem fileItem = new DiskFileItem("fileData", "application/pdf",true, outputFile.getName(), 100000000, new java.io.File(System.getProperty("java.io.tmpdir")));
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
접근법 2 :
는 사용자 정의 다중 파일 객체를 생성하고 바이트 배열의 MultipartFile을 변환합니다.
public class CustomMultipartFile implements MultipartFile {
private final byte[] fileContent;
private String fileName;
private String contentType;
private File file;
private String destPath = System.getProperty("java.io.tmpdir");
private FileOutputStream fileOutputStream;
public CustomMultipartFile(byte[] fileData, String name) {
this.fileContent = fileData;
this.fileName = name;
file = new File(destPath + fileName);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
fileOutputStream = new FileOutputStream(dest);
fileOutputStream.write(fileContent);
}
public void clearOutStreams() throws IOException {
if (null != fileOutputStream) {
fileOutputStream.flush();
fileOutputStream.close();
file.deleteOnExit();
}
}
@Override
public byte[] getBytes() throws IOException {
return fileContent;
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(fileContent);
}
}
위의 CustomMultipartFile 개체를 사용하는 방법입니다.
String fileName = "intermediate.pdf";
CustomMultipartFile customMultipartFile = new CustomMultipartFile(bytea, fileName);
try {
customMultipartFile.transferTo(customMultipartFile.getFile());
} catch (IllegalStateException e) {
log.info("IllegalStateException : " + e);
} catch (IOException e) {
log.info("IOException : " + e);
}
이
가 필요한 PDF를 생성하고
감사 intermediate.pdf 이름으로
java.io.tmpdir을 에 그것을 저장합니다.
Thats cool. 고마워. –
아주 좋은 해결책이 당신에 의해 주어집니다.이 질문과 대답은 많은 사람들에게 유용 할 것입니다. –
'transferTo'에서, FileOutputStream을 쓰면 닫아야합니까? – Ascalonian