java servlet getservletcontext_java – 如何在spring mvc Controller中获取getServletContext()

我需要在我的项目中上传图像。如何在spring mvc中获取上传路径。

路径是

/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” appear when I use this code 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 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");

}

三错误

>此方法必须返回ModelAndView类型的结果

>对于ServletFileUpload类型,setFileSizeMax(int)的方法未定义

>对于主页控制器类型,getServletContext()方法未定义

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值