Reputation: 457
I'm trying to upload a image to server... using JSP
In JSP:
input id="fileUpload" type="file" name="file"
In Java:
File file = new File( request.getParameter("file"));
String path = file.getAbsolutePath();
BufferedImage sourceImage = ImageIO.read(new File(path));
ImageIO.write(sourceImage, "jpg", new File("E:/h.jpg"));
If upload an image using internet explorer it works...but in chrome, Firefox and safari browser shows the filepath "fakepath/filename.jpg" and the image cant read.
Upvotes: 0
Views: 2309
Reputation: 9705
In Internet Expolorer it works more-or-less by accident, because you're probably running the application on the same machine as your accessing it from. Internet Explorer sends the real filename along when you upload a file, for example C:\Users\Administrator\Desktop\Image.jpg
. Your web application runs on the same machine so it can read that file from disk.
However, Chrome and Firefox don't want to expose the full path names from your client to the application, and they use the fakepath\Image.jpg
. There is no such file on your disk, and that's why the web appliaction can't read the image.
What you should do is extract the different parts from the request, find the part that contains the uploaded file, and read the data from the request (instead of from file). The good news is that you don't have to invent all this code yourself; you can use Apache Commons Fileupload and it will handle all the difficult parts for you.
Upvotes: 2