Results 1 to 11 of 11
Thread: problem with file upload
- 10-21-2010, 08:16 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 9
- Rep Power
- 0
problem with file upload
Hi guys,
I'm having problems when trying to upload a picture from my hard drive to my server folder which is at localhost. This is the error i'm getting:
java.io.FileNotFoundException: /Library/glassfishv3/glassfishv3/glassfish/domains/domain1/eclipseApps/LaFamily/files (Is a directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.j ava:179)
at java.io.FileOutputStream.<init>(FileOutputStream.j ava:131)
at org.apache.commons.fileupload.disk.DiskFileItem.wr ite(DiskFileItem.java:449)
at client.UploadPicture.doPost(UploadPicture.java:89)
I'm using commons.fileUpload in Eclipse and i'm runing it on Glassfish on osx
Here is my Servlet code
private static final String DESTINATION_DIR_PATH ="/files";
private File destinationDir;
public void init(ServletConfig config) throws ServletException {
super.init(config);
String realPath = getServletContext().getRealPath(DESTINATION_DIR_PA TH);
destinationDir = new File(realPath);
if(!destinationDir.isDirectory()) {
throw new ServletException(DESTINATION_DIR_PATH+" is not a directory");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory ();
fileItemFactory.setSizeThreshold(1*1024*1024); //1 MB
ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
try {
List<FileItem> items = uploadHandler.parseRequest(request);
Iterator<FileItem> itr = items.iterator();
while(itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if(item.isFormField()) {
out.println("File Name = "+item.getFieldName()+", Value = "+item.getString());
} else {
item.write(destinationDir);
}
out.close();
}
}catch(FileUploadException ex) {
log("Error encountered while parsing the request",ex);
} catch(Exception ex) {
log("Error encountered while uploading file",ex);
}
request.getRequestDispatcher("/index.jsp");
}
My JSP looks like this
<form action="UploadPicture" method="post" enctype="multipart/form-data">
<input type="file" name="myFile">
<input type="submit" value="Upload">
</form>
Got any ideas??
Thanks in advance
- 10-21-2010, 09:10 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
I'm guessing a bit here, but should the File past to here:
item.write(destinationDir);
be the destination directory plus the filename?
In fact, the answer is yes according to the API.
So you want to pass in a new File, using the File/String constructor.
- 10-21-2010, 09:19 AM #3
Member
- Join Date
- Jun 2010
- Posts
- 9
- Rep Power
- 0
Do you mean like this:
File file = new File(destinationDir,item.getName());
item.write(file);
It's not working either, only that now i get no errors but the file is not being copied :(
- 10-21-2010, 09:36 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
What's the value of getName()?
What's the value of getAbsolutePath() of the file?
Debug it, either with a debugger or simple sysout statements.
- 10-21-2010, 09:43 AM #5
Member
- Join Date
- Jun 2010
- Posts
- 9
- Rep Power
- 0
Can't check it right now, i'm at school, but what should be the right value??
- 10-21-2010, 10:17 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Just print them out and see if they look OK.
The absolute path should be where you want the file to go (ie the full path to the file).
The name should, presumably, be the name you want for the file...and not something else entirely.
- 10-21-2010, 10:58 AM #7
Member
- Join Date
- Jun 2010
- Posts
- 9
- Rep Power
- 0
The getName() is the file name and the absolutePath() is the value of destinationDir which is in my Project Root Folder and i named it /files.
- 10-21-2010, 11:27 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
You can say that, but until you print it out or debug it you may be making assumptions, because something is clearly not correct.
getAbsoultePath() for the new file should be destinationDir plus the filename, for example.
No point assuming this is the case...sysout or debug.
- 10-21-2010, 12:08 PM #9
Member
- Join Date
- Jun 2010
- Posts
- 9
- Rep Power
- 0
So i printed it and this is what i got:
INFO: name----->IMG_0335.JPG
INFO: path----->/Library/glassfishv3/glassfishv3/glassfish/domains/domain1/eclipseApps/LaFamily/files/IMG_0335.JPG
That's exactly where i want to save the file. But it's not happening. I even update my project in Eclipse and no file shows up ;(
- 10-21-2010, 12:14 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
So when you go to that directory on your system there is no file?
Is that a *nix system?
- 10-21-2010, 12:44 PM #11
Member
- Join Date
- Jun 2010
- Posts
- 9
- Rep Power
- 0
Solved
Wow this is kind of embarrassing, the file was being saved in /Library/glassfishv3/glassfishv3/glassfish/domains/domain1/eclipseApps/LaFamily/files/. Not in the folder i have for my saved files. This is kind of logic because my application server only knows its own folders, so what i can do is to write some code to move this saved file to my preferred folder. Anyways thanks Tolls for the help couldn't have done it without it. I'm posting the hole code in case that someone is looking for the same answer:
My Servlet:
package client;
import javax.servlet.annotation.WebServlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFac tory;
import org.apache.commons.fileupload.servlet.ServletFileU pload;
@WebServlet("/UploadPicture")
public class UploadPicture extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String DESTINATION_DIR_PATH ="/files";
private File destinationDir;
public UploadPicture() {
super();
}
public void init(ServletConfig config) throws ServletException {
super.init(config);
String realPath = getServletContext().getRealPath(DESTINATION_DIR_PA TH);
destinationDir = new File(realPath);
if(!destinationDir.isDirectory()) {
throw new ServletException(DESTINATION_DIR_PATH+" is not a directory");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory ();
fileItemFactory.setSizeThreshold(1*1024*1024); //1 MB
ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
try {
List<FileItem> items = uploadHandler.parseRequest(request);
Iterator<FileItem> itr = items.iterator();
while(itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if(item.isFormField()) {
out.println("File Name = "+item.getFieldName()+", Value = "+item.getString());
} else {
out.println("Field Name = "+item.getFieldName()+
", File Name = "+item.getName()+
", Content type = "+item.getContentType()+
", File Size = "+item.getSize());
File file = new File(destinationDir,item.getName());
item.write(file);
}
out.close();
}
}catch(FileUploadException ex) {
log("Error encountered while parsing the request",ex);
} catch(Exception ex) {
log("Error encountered while uploading file",ex);
}
request.getRequestDispatcher("/index.jsp");
}
}
My JSP
<form action="UploadPicture" method="post" enctype="multipart/form-data">
<input type="file" name="myFile">
<input type="submit" value="Upload">
</form>
That's it, working beautifly ;)
Thanks for the help.
Similar Threads
-
file upload in jsp
By mudit222 in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 04-20-2010, 08:10 PM -
:large file upload to server(chunk upload)
By tommy_725 in forum NetworkingReplies: 0Last Post: 10-16-2009, 12:21 AM -
File Upload Servlet problem
By jeniramires in forum Advanced JavaReplies: 3Last Post: 08-18-2008, 07:34 PM -
how to upload a file?
By tommy in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 06-30-2008, 02:50 PM -
file upload
By sundarjothi in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-27-2008, 11:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks