Results 1 to 12 of 12
  1. #1
    ibhaviik is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Lightbulb How to upload image in jsp/servlet

    I want make web page that allow user to upload image to server.
    Please help me .
    I have no idea about this ....
    Thanks..

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    Are you looking to do this in a Java(Servlet) or Javascript?

    There is a massive difference between each.
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  3. #3
    ibhaviik is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    yes i want to do this in java servlet

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Might I suggest Google?
    There's rather a lot of stuff out there for file uploading in servlets.

  5. #5
    ibhaviik is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    i tried but i didn't get my solution
    if you have program then please give me

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

  7. #7
    ibhaviik is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    I have following code but it's not working :
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html>
    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

    <head>
    <script type="text/javascript">

    </script>
    </head>
    <body>
    <!-- upload.jsp -->

    <%@ page import="java.io.*" %>
    <%
    //to get the content type information from JSP Request Header

    String contentType = request.getContentType();

    //here we are checking the content type is not equal to Null and
    //as well as the passed data from mulitpart
    //form - data
    // is greater than or
    //equal to 0
    if ((contentType != null) && (contentType.indexOf("multipart/form - data") >= 0)) {

    DataInputStream in = new DataInputStream(request.getInputStream());
    //we are taking the length of Content type data
    int formDataLength = request.getContentLength();
    byte dataBytes[] = new byte[formDataLength];
    int byteRead = 0;
    int totalBytesRead = 0;
    //this loop converting the uploaded file into byte code
    while (totalBytesRead < formDataLength) {
    byteRead = in.read(dataBytes, totalBytesRead,
    formDataLength);
    totalBytesRead += byteRead;
    }
    String file = new String(dataBytes);
    //for saving the file name
    String saveFile = file.substring(file.indexOf("filename=\"") + 10);
    saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
    saveFile = saveFile.substring(saveFile.lastIndexOf("\\")
    + 1, saveFile.indexOf("\""));
    int lastIndex = contentType.lastIndexOf("=");
    String boundary = contentType.substring(lastIndex + 1,
    contentType.length());
    int pos;
    //extracting the index of file
    pos = file.indexOf("filename=\"");
    pos = file.indexOf("\n", pos) + 1;
    pos = file.indexOf("\n", pos) + 1;
    pos = file.indexOf("\n", pos) + 1;
    int boundaryLocation = file.indexOf(boundary, pos) - 4;
    int startPos = ((file.substring(0, pos)).getBytes()).length;
    int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

    // creating a new file with the same name and writing the content in =new fileFileOutputStream

    OutputStream fileOut = new FileOutputStream("E:/"+saveFile);
    fileOut.write(dataBytes, startPos, (endPos - startPos));
    fileOut.flush();
    fileOut.close();
    %><Br>
    <table border="2">
    <tr>
    <td>
    <b>You have successfully
    upload the file by the name of:</b>
    <% out.println(saveFile);%>
    </td>
    </tr>
    </table> <%}%>


    </body>

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    That's not a servlet...it's a JSP.
    You shold only have display code in a JSP.
    Debugging that to find out what's wrong with it is a lot harder as a JSP.

    But I will ask why you are turning a perfectly useable InputStream and turning it into a DataInputStream?

  9. #9
    ibhaviik is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    yup i know that's not a servlet.
    i find this from google.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Why didn't you simply go with the Apache stuff which is the first link?
    And it has code and everything...

  11. #11
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    1. This is a rather old thread.
    2. That's not uploading an image, that's displaying an image.

Similar Threads

  1. Ajax / servlet File upload
    By n3mesi83 in forum Java Servlet
    Replies: 0
    Last Post: 06-08-2010, 02:48 PM
  2. upload and download file using servlet
    By amit_m04 in forum Java Servlet
    Replies: 1
    Last Post: 09-27-2009, 03:06 AM
  3. Replies: 6
    Last Post: 06-04-2009, 11:09 PM
  4. File Upload Servlet problem
    By jeniramires in forum Advanced Java
    Replies: 3
    Last Post: 08-18-2008, 07:34 PM
  5. how to upload a file using servlet
    By simon in forum Java Servlet
    Replies: 1
    Last Post: 08-05-2007, 03:50 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •