473,383 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

To know about file upload

Hello To Every One,
I want to know that when I upload the File like a image from html page Its not show on servlet page using appropriate logic.
I read the FileUpload Home page.But I am still confused.
Is that true that file uploaded image can not show on servlet.Its store to database or memory..??
Please assist me.
Thanks.
Dec 12 '08 #1
3 3063
JosAH
11,448 Expert 8TB
@vijaykumardahiya
All your browser does when you have included a 'input type="file"' item is send the file data together with the 'post' request. Your HttpServletRequest has a getInputStream() method that lets you read that file data. I don't understand your remark "Is it true that file uploaded image can not show on servlet". The Servlet doesn't even know that it has just read an image. You have to take care of that.

kind regards,

Jos
Dec 12 '08 #2
My issue is upload the image and display on servlet.But Servlet page not display the image properly.Although Servlet page show a small icon.and server side show Length but not show the index value,Its show -1.like that:
Expand|Select|Wrap|Line Numbers
  1. Length is: 6315
  2. ******************
  3. index is: -1
  4. ++++++++++++
Please review my code.
Here my servlet file:
Upload file.java:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.io.IOException;
  3. import java.util.Iterator;
  4. import java.util.List;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. import org.apache.commons.fileupload.DiskFileUpload;
  12. import org.apache.commons.fileupload.FileItem;
  13. import org.apache.commons.fileupload.FileItemFactory;
  14. import org.apache.commons.fileupload.FileUpload;
  15. import org.apache.commons.fileupload.FileUploadException;
  16.  
  17. public class UploadFile extends HttpServlet {
  18.  
  19. public void doPost(HttpServletRequest req,HttpServletResponse res)
  20. {
  21. try    
  22.     { 
  23. FileUpload fup=new FileUpload();
  24. boolean isMultipart = FileUpload.isMultipartContent(req);
  25. // Create a new file upload handler
  26. System.out.println(isMultipart);
  27. DiskFileUpload upload = new DiskFileUpload();
  28.  
  29. // Parse the request
  30. List  items = upload.parseRequest(req);
  31.  
  32. Iterator iter = items.iterator();
  33. while (iter.hasNext()) 
  34.         {
  35.  
  36. FileItem item = (FileItem) iter.next();
  37. if (item.isFormField())     {
  38. System.out.println("its a field");
  39.             }
  40.  else 
  41.             {
  42. System.out.println("its a file");
  43. System.out.println(item.getName());
  44. File cfile=new File(item.getName());
  45. File tosave=new File(getServletContext().getRealPath("/"),cfile.getName());
  46. item.write(tosave);
  47. String contentType = req.getContentType();
  48. System.out.println("Content type is :: " +contentType);
  49. if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) 
  50.         {
  51. InputStream in = req.getInputStream();
  52. int len = req.getContentLength();
  53. System.out.println("Length is: "+len);
  54. byte [] dataBytes = new byte[len];
  55. int index=in.read(dataBytes, 1, len);
  56. System.out.println("********");
  57. System.out.println("index is: "+index);
  58. System.out.println("+++++++++");
  59.              res.reset();
  60. res.setContentType("image/gif");
  61.      res.getOutputStream().write(dataBytes, 0, len);
  62. res.getOutputStream().flush();    
  63.          System.out.println("Suceesfully displayed");
  64.             }
  65.             }
  66.         }
  67.     }    
  68. catch(Exception e){System.out.println(e);}
  69. }
  70. }
Please Suggest me.
Dec 12 '08 #3
Hello Java Experts,
My Requirement is when I upload the File like a Image from a Html page,It should be display on Servlet.
But at run time when I upload the image and click on submit button It not display on servlet,Its appear a downloaded form and downloaded on disk when click on save button.Other thing is that It not a .jpg form.that mean It does not open by photo gallery.that message show file format not supported by Photo Gallary.
Here my files:
Please review:
Home.html:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <form method="post" action="tmp" enctype="multipart/form-data">
  3. Name
  4. <input type="text" name="name"/>
  5. File
  6. <input type="file" name="file"/>
  7. <input type="submit"/>
  8. </form>
  9. </html>
  10.  
UploadFile.java:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.io.IOException;
  3. import java.util.Iterator;
  4. import java.util.List;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. import org.apache.commons.fileupload.DiskFileUpload;
  12. import org.apache.commons.fileupload.FileItem;
  13. import org.apache.commons.fileupload.FileItemFactory;
  14. import org.apache.commons.fileupload.FileUpload;
  15. import org.apache.commons.fileupload.FileUploadException;
  16. import javax.activation.DataSource;
  17. public class UploadFile extends HttpServlet {
  18.  
  19. public void doPost(HttpServletRequest req,HttpServletResponse res)
  20. {
  21. try{
  22. FileUpload fup=new FileUpload();
  23. boolean isMultipart = FileUpload.isMultipartContent(req);
  24. // Create a new file upload handler
  25. System.out.println(isMultipart);
  26. DiskFileUpload upload = new DiskFileUpload();
  27.  
  28. // Parse the request
  29. List  items = upload.parseRequest(req);
  30.  
  31. Iterator iter = items.iterator();
  32. while (iter.hasNext()) {
  33.  
  34. FileItem item = (FileItem) iter.next();
  35. InputStream in=item.getInputStream();
  36. int len = req.getContentLength();
  37.  System.out.println("*************");
  38.  System.out.println("Length is: "+len); 
  39.  System.out.println("+++++++++++");
  40. byte [] dataBytes = new byte[len]; 
  41. int index=in.read(dataBytes, 0, len);
  42. System.out.println("********");
  43.  System.out.println("index"+index);
  44. System.out.println("+++++++++");
  45. //  res.reset();
  46. res.setContentType("image/jpg");
  47.      res.getOutputStream().write(dataBytes,0,len);
  48. res.getOutputStream().flush();  
  49. System.out.println("Suceesfully send");
  50. }
  51.  
  52. }
  53. catch(Exception e){System.out.println(e);}
  54. }
  55. }
Please help me I try this program from a long time But I am not suceed.
Dec 13 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tihon | last post by:
Hello! I again need your help, just can't understand whats going on. Got this upload pictures form and it's having problem handling large files (~1.5 - 2 MB). Everything works fine if i just...
15
by: Simon | last post by:
I would like to create a very basic file upload add image form to add to my web site and to keep them in a "tmp" directory within my web hosting file manager once uploaded. I understand the basic...
2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
2
by: mark | last post by:
How do I detect that a particular form element is a file upload or if the file upload has worked? In the Python cgi module documentation I found suggested code... form = cgi.FieldStorage()...
4
by: Marko Vuksanovic | last post by:
I am trying to cause the uplaod button, id="Upload",when clicked, to exectue the onClick event for Button1, id="Button1". <asp:FileUpload id="FileUpload" runat="server"> </asp:FileUpload>...
1
by: DavidA | last post by:
I have a very simple form and perl script that is to upload a jpg file. I am not familiar with the perl language but copied the code from a text book. It works fine with all browsers except IE....
7
by: pbd22 | last post by:
hi. i am having probs understanding how to grab a file being uploaded from a remote client. i am using hidden input fields for upload such as: <input id="my_file_element" type="file"...
2
by: hotflash | last post by:
Hi All, I found the best pure ASP code to upload a file to either server and/or MS Access Database. It works fine for me however, there is one thing that I don't like and have tried to fix but...
1
by: chrisj | last post by:
I'm using freeASPupload and got some assistance integrating to a Member script. It works successfully. In this modified version there are two groups that use this upload script. Members of one...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.