gpt4 book ai didi

java - 如何使用 spring mvc 将图像上传到 webapp/resources/images 目录?

转载 作者:搜寻专家 更新时间:2023-11-01 04:02:56 25 4
gpt4 key购买 nike

我有存储库类,我想在将产品详细信息添加到存储库之前将来自 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);
}

我的存储库类是 ResourceLoaderAware。我收到 fileNotFoundException,“imagesDesert.jpg”是我要上传的图片

java.io.FileNotFoundException: webapp\resources\imagesDesert.jpg (The system cannot find the path specified)

最佳答案

这个 Controller (由 https://askgif.com/ 提供)对我来说工作正常。

来源:https://askgif.com/blog/126/how-can-i-upload-image-using-spring-mvc-java/

试试这个

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";
}
}

关于java - 如何使用 spring mvc 将图像上传到 webapp/resources/images 目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19922358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com