File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes getting the uploaded filename for downloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "getting the uploaded filename for downloading" Watch "getting the uploaded filename for downloading" New topic
Author

getting the uploaded filename for downloading

indu yeturu
Ranch Hand

Joined: Aug 27, 2008
Posts: 123
Hi,
am uploading a file, and after its uploading, the link will go for downloading. Here it is getting the file name as "null" instead of uploaded filename. can you please tell me how the parameters of multi-part/formdata are stored. please i stuck with this from 2 days.

thanks
indu
[ September 04, 2008: Message edited by: Bear Bibeault ]

thanks,
indu
Karup Mew
Ranch Hand

Joined: Aug 11, 2007
Posts: 40
Can you post your code snippet here.
indu yeturu
Ranch Hand

Joined: Aug 27, 2008
Posts: 123
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
response.setContentType("text/html");
///out.println("upload servlet... POST ***** ");
boolean ismultipart = ServletFileUpload.isMultipartContent(request);
try
{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
DiskFileItemFactory dfactory= new DiskFileItemFactory();
List items=upload.parseRequest(request);
Iterator iter = items.iterator();

while(iter.hasNext())
{
FileItem item = (FileItem)iter.next();

String fieldname = item.getFieldName();
HttpSession session = request.getSession();
session.setAttribute("filename",fieldname);
// out.println("field name --->>> "+fieldname);
String fieldvalue = item.getName();
// out.println("<br>field value --->>> "+fieldvalue);
session.setAttribute("fieldvalue",fieldvalue);
String contenttype=item.getContentType();
long size = item.getSize();
boolean memory = item.isInMemory();
//String filename=request.getParameter("filename");
//out.println(filename);
File saveTo = new File("/upload_files/myFile.txt");
item.write(saveTo);

}


}
catch(Exception e)
{
e.printStackTrace();
}
RequestDispatcher disp=request.getRequestDispatcher("UploadSuccess.jsp");
disp.forward(request,response);
}
indu yeturu
Ranch Hand

Joined: Aug 27, 2008
Posts: 123
THis is my uploaded logic, is this is correct way of writing in to a server ?
thanks
indu
Karup Mew
Ranch Hand

Joined: Aug 11, 2007
Posts: 40
List items=upload.parseRequest(request);


Check the List object contains the FileItem object
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61217
    
  66

Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the .


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
indu yeturu
Ranch Hand

Joined: Aug 27, 2008
Posts: 123
oh, sorry Bear, thanks for your suggestion...
indu
indu yeturu
Ranch Hand

Joined: Aug 27, 2008
Posts: 123
Karup, the List item is not having FileItem object..
Karup Mew
Ranch Hand

Joined: Aug 11, 2007
Posts: 40
Check whether the request object Content Type is multipart/formdata or not.
If possible show the console output also.
[ September 05, 2008: Message edited by: Karup Mew ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting the uploaded filename for downloading