如何更改保存文件目录?
How can I change saving file directory?
我想更改保存文件目录。
这是我的代码:
@RequestMapping(value = "/UploadFile")
public String uploadFile(HttpServletResponse response String base64, String name, String size) throws Exception {
byte[] decodedFile = Base64.getDecoder().decode(base64.getBytes(StandardCharsets.UTF_8));
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + name);
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
InputStream is = new ByteArrayInputStream(decodedFile);
IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
return "true";
}
试试这个:
File path = new File("YOUR PATH HERE") ;
try (FileOutputStream fos = new FileOutputStream(path)) {
fos.write(decodedFile);
} catch (IOException e) {
e.printStackTrace();
}
您还应该考虑阅读这些链接:
https://medium.com/javarevisited/how-to-upload-files-to-local-directory-in-spring-boot-c8c33f8239d3
https://spring.io/guides/gs/uploading-files/
编辑
如果字符串名称是原始文件名,你也可以这样做:
File path = new File("C:\YOUR_PATH\images\" + name);
我想更改保存文件目录。 这是我的代码:
@RequestMapping(value = "/UploadFile")
public String uploadFile(HttpServletResponse response String base64, String name, String size) throws Exception {
byte[] decodedFile = Base64.getDecoder().decode(base64.getBytes(StandardCharsets.UTF_8));
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + name);
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
InputStream is = new ByteArrayInputStream(decodedFile);
IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
return "true";
}
试试这个:
File path = new File("YOUR PATH HERE") ;
try (FileOutputStream fos = new FileOutputStream(path)) {
fos.write(decodedFile);
} catch (IOException e) {
e.printStackTrace();
}
您还应该考虑阅读这些链接:
https://medium.com/javarevisited/how-to-upload-files-to-local-directory-in-spring-boot-c8c33f8239d3
https://spring.io/guides/gs/uploading-files/
编辑
如果字符串名称是原始文件名,你也可以这样做:
File path = new File("C:\YOUR_PATH\images\" + name);