Java 如何使用 spring mvc 将图像上传到 webapp/resources/images 目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19922358/
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
How to upload an image to webapp/resources/images directory using spring mvc?
提问by Amuthan
I have repository class where I want to store the image that is coming from MultipartFile to webapp/resources/images directory before adding the product details to the repository ?
我有存储库类,我想在将产品详细信息添加到存储库之前将来自 MultipartFile 的图像存储到 webapp/resources/images 目录?
@Override
public void addProduct(Product product) {
Resource resource = resourceLoader.getResource("file:webapp/resources/images");
MultipartFile proudctImage = product.getProudctImage();
try {
proudctImage.transferTo(new File(resource.getFile()+proudctImage.getOriginalFilename()));
} catch (IOException e) {
throw new RuntimeException(e);
}
listOfProducts.add(product);
}
my repository class is ResourceLoaderAware. I am getting fileNotFoundException, "imagesDesert.jpg" is the image I am trying to upload
我的存储库类是 ResourceLoaderAware。我收到 fileNotFoundException,“imagesDesert.jpg”是我要上传的图片
java.io.FileNotFoundException: webapp\resources\imagesDesert.jpg (The system cannot find the path specified)
采纳答案by Amuthan
Finally I could able to fix this problem, we need not specify the file:prefix, and moreover by default resourceLoader.getResource()
will look for resources in the root directory of our web application, So no need of specifying the webapp
folder name. So finally the following code works for me
最后我可以解决这个问题,我们不需要指定file:前缀,而且默认情况下resourceLoader.getResource()
会在我们的 web 应用程序的根目录中查找资源,所以不需要指定webapp
文件夹名称。所以最后下面的代码对我有用
@Override
public void addProduct(Product product) {
MultipartFile proudctImage = product.getProudctImage();
if (!proudctImage.isEmpty()) {
try {
proudctImage.transferTo(resourceLoader.getResource("resources/images/"+product.getProductId()+".png").getFile());
} catch (Exception e) {
throw new RuntimeException("Product Image saving failed", e);
}
}
listOfProducts.add(product);
}
回答by Sumit Chourasia
this controller (courtesy: https://askgif.com/) is working fine for me.
这个控制器(礼貌:https: //askgif.com/)对我来说工作正常。
source: https://askgif.com/blog/126/how-can-i-upload-image-using-spring-mvc-java/
来源:https: //askgif.com/blog/126/how-can-i-upload-image-using-spring-mvc-java/
try this
尝试这个
package net.viralpatel.spring3.controller;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.viralpatel.spring3.form.FileUploadForm;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class FileUploadController {
//private String saveDirectory = "D:/Test/Upload/"; //Here I Added
private String saveDirectory = null; //Here I Added
@RequestMapping(value = "/show", method = RequestMethod.GET)
public String displayForm() {
return "file_upload_form";
}
@SuppressWarnings("null")
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(
@ModelAttribute("uploadForm") FileUploadForm uploadForm,
Model map,HttpServletRequest request) throws IllegalStateException, IOException{
List<MultipartFile> files = uploadForm.getFiles();
List<String> fileUrl = new ArrayList<String>();;
String fileName2 = null;
fileName2 = request.getSession().getServletContext().getRealPath("/");
saveDirectory = fileName2+"images\";
List<String> fileNames = new ArrayList<String>();
//System.out.println("user directory : "+System.getProperty("user.dir"));
System.out.println("applied directory : " + saveDirectory);
if(null != files && files.size() > 0) {
for (MultipartFile multipartFile : files) {
String fileName = multipartFile.getOriginalFilename();
System.out.println("applied directory : " + saveDirectory+fileName);
if(!"".equalsIgnoreCase(fileName)){
//Handle file content - multipartFile.getInputStream()
fileUrl.add(new String(saveDirectory + fileName));
multipartFile.transferTo(new File(saveDirectory + fileName)); //Here I Added
fileNames.add(fileName);
}
//fileNames.add(fileName);
//Handle file content - multipartFile.getInputStream()
//multipartFile.transferTo(new File(saveDirectory + multipartFile.getOriginalFilename())); //Here I Added
}
}
map.addAttribute("files", fileNames);
map.addAttribute("imageurl",fileUrl);
return "file_upload_success";
}
}