Java 使用 Spring 将文件保存到资源目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21063140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving file to resource directory using Spring
提问by Petr Shypila
I have this project structure:
我有这个项目结构:
/webapp
/res
/img
/profile.jpg
/WEB-INF
And I need to save file to res/img/
directory. This time I have this code:
我需要将文件保存到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();
}
But it saving files to user.dir
directory, which is ~/Work/Tomcat/bin/
.
So how I can upload files to res
directory?
但它将文件保存到user.dir
目录,即~/Work/Tomcat/bin/
. 那么如何将文件上传到res
目录?
采纳答案by Ned
You shouldn't really be uploading files there.
你真的不应该在那里上传文件。
If you are using a war, redeploying will delete them. If they are intended to be temporary then use an os assigned temporary location.
如果您正在使用战争,重新部署将删除它们。如果它们是临时的,则使用操作系统分配的临时位置。
If you intend to publish them afterwards then choose a location in which to store the files on your server, make this location known to the application and save and load files from the location.
如果您打算之后发布它们,则选择一个位置来存储服务器上的文件,使应用程序知道该位置并从该位置保存和加载文件。
If you are trying to replace resources dynamically such as an image which is referenced in the html or css templates, then consider publishing the external location separately, you can use mvc:resources for this e.g:
如果您尝试动态替换资源,例如在 html 或 css 模板中引用的图像,则考虑单独发布外部位置,您可以使用 mvc:resources 来实现,例如:
<mvc:resources mapping="/images/**" location="file:/absolute/path/to/image/dir"/>
and you would save your files to that location. This will make it more permanent between deployments.
并且您会将文件保存到该位置。这将使其在部署之间更加持久。
To save an image to that location using your code you will need to add this into your bean definition (assuming you are using xml configuration without annotations):
要使用您的代码将图像保存到该位置,您需要将其添加到 bean 定义中(假设您使用的是没有注释的 xml 配置):
<property name="imagesFolder" value="/absolute/path/to/image/dir"/>
and keeping your code as similar as possible change it to:
并保持您的代码尽可能相似,将其更改为:
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();
}
Please bear in mind that you need to change /absolute/path/to/image/dir to an actual path that exists, also I would recommend to look at the Spring Resources documentationfor a better way to deal with files and resources.
请记住,您需要将 /absolute/path/to/image/dir 更改为存在的实际路径,我还建议您查看Spring Resources 文档以更好地处理文件和资源。
回答by user3556304
Please refer FileUploadController from here to save file to the specified directory.
请参考此处的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();
}