[英]Saving file to resource directory using Spring
我有這個項目結構:
/webapp
/res
/img
/profile.jpg
/WEB-INF
我需要將文件保存到res/img/
目錄。 這次我有這個代碼:
public String fileUpload(UploadedFile uploadedFile) {
InputStream inputStream = null;
OutputStream outputStream = null;
MultipartFile file = uploadedFile.getFile();
String fileName = file.getOriginalFilename();
File newFile = new File("/res/img/" + fileName);
try {
inputStream = file.getInputStream();
if (!newFile.exists()) {
newFile.createNewFile();
}
outputStream = new FileOutputStream(newFile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
}
return newFile.getAbsolutePath();
}
但它將文件保存到user.dir
目錄,即~/Work/Tomcat/bin/
。 那么如何將文件上傳到res
目錄?
你不應該真的在那里上傳文件。
如果您正在使用戰爭,重新部署將刪除它們。 如果它們是臨時的,那么使用os分配的臨時位置。
如果您打算在之后發布它們,請選擇在服務器上存儲文件的位置,使應用程序知道此位置並從該位置保存和加載文件。
如果您嘗試動態替換資源(例如html或css模板中引用的圖像),那么考慮單獨發布外部位置,您可以使用mvc:resources來實現此目的:
<mvc:resources mapping="/images/**" location="file:/absolute/path/to/image/dir"/>
並且您將文件保存到該位置。 這將使部署之間更加永久。
要使用代碼將圖像保存到該位置,您需要將其添加到bean定義中(假設您使用的是沒有注釋的xml配置):
<property name="imagesFolder" value="/absolute/path/to/image/dir"/>
並保持您的代碼盡可能相似,將其更改為:
private String imagesFolder;
public void setImagesFolder(String imagesFolder) {
this.imagesFolder = imagesFolder;
}
public String fileUpload(UploadedFile uploadedFile) {
InputStream inputStream = null;
OutputStream outputStream = null;
MultipartFile file = uploadedFile.getFile();
String fileName = file.getOriginalFilename();
File newFile = new File(imagesFolder + fileName);
try {
inputStream = file.getInputStream();
if (!newFile.exists()) {
newFile.createNewFile();
}
outputStream = new FileOutputStream(newFile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
}
return newFile.getAbsolutePath();
}
請記住,您需要將/ absolute / path / to / image / dir更改為存在的實際路徑,我還建議您查看Spring Resources文檔以獲得更好的方法來處理文件和資源。
請從此處參考FileUploadController以將文件保存到指定目錄。
public String fileUpload(UploadedFile uploadedFile) {
InputStream inputStream = null;
OutputStream outputStream = null;
MultipartFile file = uploadedFile.getFile();
String rootPath = System.getProperty("user.dir");
File dir = new File(rootPath + File.separator + "webapp"+File.separator+"res"+File.separator+"img");
if (!dir.exists())
dir.mkdirs();
String fileName = file.getOriginalFilename();
File serverFile = new File(dir.getAbsolutePath() + File.separator + fileName);
try {
inputStream = file.getInputStream();
if (!newFile.exists()) {
newFile.createNewFile();
}
outputStream = new FileOutputStream(newFile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
}
return newFile.getAbsolutePath();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.