gpt4 book ai didi

java - 如何在spring mvc Controller 中获取getServletContext()

转载 作者:IT老高 更新时间:2023-10-28 20:44:44 25 4
gpt4 key购买 nike

我需要在我的项目中上传图片。 SpringMVC中如何获取上传路径。路径是;

/home/cme/project/eclipse/workspace_12_11/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/fileUploadTester/upload

以下错误;

The method getServletContext() is undefined for the type HomePageController

在我使用此代码时出现;

String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY;

我的代码是

public ModelAndView UploadPhoto(@ModelAttribute User user, HttpServletRequest request, HttpServletResponse response) throws IOException {
final String UPLOAD_DIRECTORY = "upload";
final int THRESHOLD_SIZE = 1024 * 1024 * 3; // 3MB
final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB
final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; // 50MB

String value[] = new String[10];
int i = 0;

// checks if the request actually contains upload file
if (!ServletFileUpload.isMultipartContent(request)) {
PrintWriter writer = response.getWriter();
writer.println("Request does not contain upload data");
writer.flush();
return; //here is error This method must return a result of type ModelAndView
}

DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(THRESHOLD_SIZE);
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(MAX_FILE_SIZE); //here error The method setFileSizeMax(int) is undefined for the type ServletFileUpload
upload.setSizeMax(MAX_REQUEST_SIZE);
String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY; // here error The method getServletContext() is undefined for the type Homepage Controller
// creates the directory if it does not exist
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}

try {
List < FileItem > items = upload.parseRequest(request); // request is HttpServletRequest
for (FileItem item: items) {
if (item.isFormField()) { // text fields, etc...
String fieldName = item.getFieldName();
System.out.print("fieldname" + fieldName);
value[i] = item.getString();
System.out.print("from uploader" + value[i]);
i++;
} else {
//String fileName=new File(item.getName()).getName(); Use this to use default file name
String name = value[0];
System.out.println("file uploader name" + name);
String filePath = uploadPath + File.separator + name;
System.out.println(filePath);
File storeFile = new File(filePath);
try {
item.write(storeFile);
} catch (Exception ex) {
}
}
}
System.out.println("uploaded successfully");
} catch (Exception ex) {
System.out.println("error not uploaded");
}
return new ModelAndView("ChangePhoto");
}

三个错误

  1. 此方法必须返回 ModelAndView 类型的结果
  2. 没有为 ServletFileUpload 类型定义方法 setFileSizeMax(int)
  3. 没有为 Homepage Controller 类型定义方法 getServletContext()

最佳答案

  1. 使用下面的代码在 SpringMVC 中 Autowiring ServletContext 对象

    @Autowired
    ServletContext context;

    然后尝试像这样执行你的代码

    String uploadPath = context.getRealPath("") + File.separator + UPLOAD_DIRECTORY;
  2. 您可以像这样在 Controller 中获取它;

    private ServletContext context;

    public void setServletContext(ServletContext servletContext) {
    this.context = servletContext;
    }

    但为此,您的 Controller 必须实现 ServletContextAware 接口(interface)

关于java - 如何在spring mvc Controller 中获取getServletContext(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26923907/

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