开发者

jsp problem request.getparameter

开发者 https://www.devze.com 2023-03-05 02:52 出处:网络
I am using <input type=\"file\" name=开发者_开发知识库\"file\" value=\"\"> to browse the image file for upload. But when I use

I am using

<input type="file" name=开发者_开发知识库"file" value="">

to browse the image file for upload. But when I use

 String imageUrl = request.getParameter("file");
 out.println("logofile" + imageUrl);

at the action page, it only shows the image name not the full absolute path. When I try to use

File file = new File(imageUrl);

it throws the following exception

java.io.FileNotFoundException: apple-logo.jpg (The system cannot find the file specified) 

What am I doing wrong?


The problem is that you're trying to get the file content from the webserver's local disk file system by the name sent by the webbrowser. This is utterly wrong. Only Internet Explorer manifests the bug that it sends the full path instead of only the name. The full path is however useless to you since the webserver does normally not have access to the client's local disk file system.

You should instead get the real file content from the request body which is sent by the webbrowser. To achieve this, you need to ensure that your HTML form has the method="post" and enctype="multipart/form-data" attributes.

<form action="upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" />
</form>

Then, in the doPost() method of the servlet which is listening on URL pattern of /upload, use HttpServletRequest#getParts() or when you're still on Servlet 2.5 or older, use Apache Commons FileUpload to process the parts of the multipart/form-data request. It'll contain the uploaded file among the usual request parameters.

See also:

  • How to upload files in JSP/Servlet?
  • How to get path from file field in Firefox?


You can user check here to user Apache Commons File Upload.

0

精彩评论

暂无评论...
验证码 换一张
取 消