SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: jsp uload

  1. #1
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question jsp uload

    hi,

    I am novice in jsp, I have created one application which is for uploading file in jsp. Here is a first page for uploading file on server means on another computer.

    client side code is below. Should I do this client side functionality in jsp. means I donot want use form. How can I send or post data on server side using jsp.Or any solution for this...
    ----------------------------------------------------
    <form name="frmhide" action="http://ipadress:8080/testapp/server.jsp" method="post">
    <label>Do you want to send data</label>
    <input type="file"/>
    <input type="submit" value="Send" />
    </form>

    ---------------------------------------------------


    server side coding for saving the content of file on server.
    --------------------------------------------------
    String contentType = request.getContentType();
    //here we are checking the content type is not equal to Null and
    //s 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 file

    FileOutputStream fileOut = new FileOutputStream(saveFile);
    fileOut.write(dataBytes, startPos, (endPos - startPos));

    fileOut.flush();
    fileOut.close();
    ---------------------------------------------------


    Thanks in Advance.

  2. #2
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Package them into a war file and upload it.


Tags for this Thread

Bookmarks

Posting Permissions

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