1

I have been trying to upload a file to a Tomcat server through a servlet, but without success. I am able to create a directory, but somehow the servlet code is not creating a file inside the "upload" directory.

Java code in Android:

 public void upLoad()
{


     String exsistingFileName = path+"//"+"test123.txt";

     String lineEnd = "\r\n";
     String twoHyphens = "--";
     String boundary = "*****";
     try {
         // ------------------ CLIENT REQUEST

         Log.e(Tag, "Inside second Method");

         FileInputStream fileInputStream = new FileInputStream(new File(
                 exsistingFileName));

         // open a URL connection to the Servlet

         URL url = new URL(urlString);

         // Open a HTTP connection to the URL

         conn = (HttpURLConnection) url.openConnection();

         // Allow Inputs
         conn.setDoInput(true);

         // Allow Outputs
         conn.setDoOutput(true);

         // Don't use a cached copy.
         conn.setUseCaches(false);

         // Use a post method.
         conn.setRequestMethod("POST");

         conn.setRequestProperty("Connection", "Keep-Alive");

         conn.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);


         DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

         dos.writeBytes(twoHyphens + boundary + lineEnd);
         dos.writeBytes("Content-Disposition: post-data; name=uploadedfile;filename="
                         + exsistingFileName + "" + lineEnd);
         dos.writeBytes(lineEnd);

         Log.e(Tag, "Headers are written");

         // create a buffer of maximum size

         int bytesAvailable = fileInputStream.available();
         int maxBufferSize = 1000;
         // int bufferSize = Math.min(bytesAvailable, maxBufferSize);
         byte[] buffer = new byte[bytesAvailable];

         // read file and write it into form...

         int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);

         while (bytesRead > 0) {
             dos.write(buffer, 0, bytesAvailable);
             bytesAvailable = fileInputStream.available();
             bytesAvailable = Math.min(bytesAvailable, maxBufferSize);
             bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
         }

         // send multipart form data necesssary after file data...

         dos.writeBytes(lineEnd);
         dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

         // close streams
         Log.e(Tag, "File is written");
         fileInputStream.close();
         dos.flush();
         dos.close();

     } catch (MalformedURLException ex) {
         Log.e(Tag, "error: " + ex.getMessage(), ex);
     }

     catch (IOException ioe) {
         Log.e(Tag, "error: " + ioe.getMessage(), ioe);
     }

     try {
         BufferedReader rd = new BufferedReader(new InputStreamReader(conn
                 .getInputStream()));
         String line;
         while ((line = rd.readLine()) != null) {
             Log.e("Dialoge Box", "Message: " + line);
         }
         rd.close();

     } catch (IOException ioex) {
         Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
     }
}

Servlet code on Tomcat server:

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
* Servlet implementation class UploadServlet
*/
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private static final String UPLOAD_DIRECTORY = "upload";
private static final int THRESHOLD_SIZE = 1024 * 1024 * 3; // 3MB
private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB
private static final int REQUEST_SIZE = 1024 * 1024 * 50; // 50MB

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
 *      response)
 */
 protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // checks if the request actually contains upload file
    if (!ServletFileUpload.isMultipartContent(request)) {
        // if not, we stop here
        return;
    }

    // configures some settings
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setSizeThreshold(THRESHOLD_SIZE);
    factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setFileSizeMax(MAX_FILE_SIZE);
    upload.setSizeMax(REQUEST_SIZE);

    // constructs the directory path to store upload file
    String uploadPath = getServletContext().getRealPath("")
        + File.separator + UPLOAD_DIRECTORY;
    // creates the directory if it does not exist
    File uploadDir = new File(uploadPath);
    if (!uploadDir.exists()) {
        uploadDir.mkdir();   //I can see the created directory...but nothing happens after that....No error message display after creating directory
    }

    try {
        // parses the request's content to extract file data
        List formItems = upload.parseRequest(request);
        Iterator iter = formItems.iterator();

        // iterates over form's fields
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
            // processes only fields that are not form fields
            if (!item.isFormField()) {
                String fileName = new File(item.getName()).getName();
                String filePath = uploadPath + File.separator + fileName;
                File storeFile = new File(filePath);

                // saves the file on disk
                item.write(storeFile);
            }
        }
        request.setAttribute("message", "Upload has been done successfully!");
    } catch (Exception ex) {
        request.setAttribute("message", "There was an error: " + ex.getMessage());
    }
    getServletContext().getRequestDispatcher("/message.jsp").forward(request,         response);
 }
}

My file structure on the server is:

/webapps/
   demo/
      upload/ - The file I'm trying to create (test123.txt) should be inside this folder, but it's not.
   WEB-INF/
      classes/ - UploadServlet.class
      lib/ - commons IO and upload jars
hopper
  • 12,140
  • 7
  • 48
  • 50
Shikha Shah
  • 743
  • 2
  • 12
  • 31
  • 1
    Atleast let me know the reason who downvoted this question...I have been doing this since 2 days..you should better think twice...this is outrageous..!! – Shikha Shah Nov 28 '12 at 21:37
  • not me, :) ... Some error, logs, debug, fails? Its not 'Hello world' program and we can't debug client/server side. Please add more info. – Maxim Shoustin Nov 28 '12 at 21:37
  • We need more information. Also, try to use some standard librairies better than uploading the file yourself. Google Http Java Client or Spring Rest Client could fit. – Snicolas Nov 28 '12 at 21:40
  • I am not able to paste entire log...but from android side i get logs "Headers are written", "File is written" but later it gives error that it doesnt found file to read , which is right cause file has not been created...no logs from servlet.. – Shikha Shah Nov 28 '12 at 21:41
  • Please let me know what more information you guys need...!! – Shikha Shah Nov 28 '12 at 21:43
  • Agree with @Snicolas, try to upload manually through FTP. or write some dummy JSP page to upload file (button 'upload' from path). Really hard without Servlet logs ;/ – Maxim Shoustin Nov 28 '12 at 21:45
  • As far as I get after creating directory it is not able to execute this line "List formItems = upload.parseRequest(request);" but it doesnt even gives any error..i really need to get this done...I cant use FTP..:( – Shikha Shah Nov 28 '12 at 21:50
  • btw, if you send basic `String` all work fine, true? – Maxim Shoustin Nov 28 '12 at 21:50
  • dint get you...after creating directory "upload" I dont see any error from servlet side... – Shikha Shah Nov 28 '12 at 21:51
  • As I understand u doesn't get `FileUploadException` on parseRequest and your list is not empty. Anyways take a look on: http://stackoverflow.com/questions/4364882/send-objects-from-flex-to-java-servlet and on: http://stackoverflow.com/questions/8752469/why-doesnt-servlet-find-fileitem-in-multipart-request – Maxim Shoustin Nov 28 '12 at 21:54
  • boundary seems strange to me. Checkout another example. – Snicolas Nov 28 '12 at 22:00
  • @Snicolas : this same code works when i use php on server side...:( – Shikha Shah Nov 28 '12 at 22:12
  • so show your server logs – Snicolas Nov 28 '12 at 23:44

1 Answers1

1

Your Content-Disposition header is wrong. It says post-data while it has to be form-data in case of multipart/form-data. Fix it accordingly in your client:

dos.writeBytes("Content-Disposition: form-data; name=uploadedfile;filename="
                     + exsistingFileName + "" + lineEnd);

See also:


Unrelated to the concrete problem, storing uploaded files in the webapp's deploy folder is absolutely a bad idea. They'll all get lost whenever you redeploy a new version of the webapp, with the simple reason that all those so far uploaded files are for sure not contained in the original WAR. Store them somewhere else outside the deploy folder. Just do not ever use getRealPath(), it'll only lead to poor practices.

Further, FileInputStream#available() absolutely doesn't do what you thought there in your code. Get rid of it and just do the usual read-write in a for loop with a fixed buffer size, e.g. 10KB.

Also, using DataOutputStream here is scary. It serves a different purpose (creating .dat files) and in this particular construct there's a big risk for character encoding related issues. Just use PrintWriter which you wrap around OutputStreamWriter with a specified charset. Note that a complete example is shown in the above "See also" link.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452