title: android asynctask upload file success ( no error) but cant see in server
body: <p>Hello to day I find a way to upload database to my localhost.
  here is my Serverlet </p>
  
  <pre><code>@WebServlet("/UploadDownloadFileServlet")
  
  public class UploadDownloadFileServlet extends HttpServlet {
  
  private static final long serialVersionUID = 1L;
  private ServletFileUpload uploader = null;
  
  @Override
  public void init() throws ServletException {
      DiskFileItemFactory fileFactory = new DiskFileItemFactory();
      File filesDir = (File) getServletContext().getAttribute(
              "FILES_DIR_FILE");
      fileFactory.setRepository(filesDir);
      this.uploader = new ServletFileUpload(fileFactory);
  }
  
  protected void doGet(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException {
      String fileName = request.getParameter("fileName");
      if (fileName == null || fileName.equals("")) {
          throw new ServletException("File Name can't be null or empty");
      }
      File file = new File(request.getServletContext().getAttribute(
              "FILES_DIR")
              + File.separator + fileName);
      if (!file.exists()) {
          throw new ServletException("File doesn't exists on server.");
      }
      System.out.println("File location on server::" + file.getAbsolutePath());
      ServletContext ctx = getServletContext();
      InputStream fis = new FileInputStream(file);
      String mimeType = ctx.getMimeType(file.getAbsolutePath());
      response.setContentType(mimeType != null ? mimeType : "application/octet-stream");
      response.setContentLength((int) file.length());
      response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
  
      ServletOutputStream os = response.getOutputStream();
      byte[] bufferData = new byte[1024];
      int read = 0;
      while ((read = fis.read(bufferData)) != -1) {
          os.write(bufferData, 0, read);
      }
      os.flush();
      os.close();
      fis.close();
      System.out.println("File downloaded at client successfully");
  }
  
  protected void doPost(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException {
      if (!ServletFileUpload.isMultipartContent(request)) {
          throw new ServletException(
                  "Content type is not multipart/form-data");
      }
  
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.write("&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;");
      try {
          List&lt;FileItem&gt; fileItemsList = uploader.parseRequest(request);
          Iterator&lt;FileItem&gt; fileItemsIterator = fileItemsList.iterator();
          while (fileItemsIterator.hasNext()) {
              FileItem fileItem = fileItemsIterator.next();
              System.out.println("FieldName=" + fileItem.getFieldName());
              System.out.println("FileName=" + fileItem.getName());
              System.out.println("ContentType=" + fileItem.getContentType());
              System.out.println("Size in bytes=" + fileItem.getSize());
  
              File file = new File(request.getServletContext().getAttribute(
                      "FILES_DIR")
                      + File.separator + fileItem.getName());
              System.out.println("Absolute Path at server="
                      + file.getAbsolutePath());
              fileItem.write(file);
              }
  }
  </code></pre>
  
  <p>yes, I then the code from android :</p>
  
  <pre><code>class DatabaseAction extends AsyncTask&lt;Void, Integer, Void&gt; {
      SQLiteDatabase db;
      // String link =
      // "http://192.168.56.101:8080/UploadServlet301/upload.jsp";
      String link = "http://192.168.56.101:8080/ServletFileUploadDownloadExample/";
      HttpURLConnection connection = null;
      DataOutputStream outputStream = null;
      DataInputStream inputStream = null;
      URL url;
      String folderPath;
      String arrayOfFiles[];
      File root;
      File allFiles;
  
      String urlServer = "http://urluploadscriptaddress.php";
      String lineEnd = "\r\n";
      String twoHyphens = "--";
      String boundary = "*****";
  
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1 * 1024 * 1024;
  
      public DatabaseAction(SQLiteDatabase db) {
          this.db = db;
      }
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          Logger.error("start");
      }
  
      @Override
      protected Void doInBackground(Void... params) {
          Logger.error("path: " + db.getPath());
          uploadFile(db.getPath());
          return null;
      }
  
      public long uploadFile(String sourceFileUri) {
          String fileName = sourceFileUri;
          long TotalSize = 0;
          HttpURLConnection comm = null;
          DataOutputStream dos = null;
          String lineEnd = "\r\n";
          String twoHyphens = "--";
          String boundary = "*****";
          int bytesRead,  bytesAvailable, bufferSize;
          byte[] buffer;
          int maxBufferSize = 10240;
          File sourceFile = new File(sourceFileUri);
          int serverResonseCode;
  
          if (!sourceFile.isFile()) {
              Log.e("UploadFile", "Source File not Exist: " + sourceFile);
  
              return 0;
          } else {
              try {
                  FileInputStream fis = new FileInputStream(db.getPath());
                  URL url = new URL(link);
  
                  comm = (HttpURLConnection) url.openConnection();
                  comm.setDoInput(true);
                  comm.setDoOutput(true);
                  comm.setUseCaches(false);
                  comm.setRequestMethod("POST");
                  comm.setRequestProperty("Connection", "Keep-Alive");
                  comm.setRequestProperty("ENCTYPE", "multipart/form-data");
                  comm.setRequestProperty("Content-Type",
                          "multipart/form-data;boundary=" + boundary);
                  comm.setRequestProperty("fileName", fileName);
                  dos = new DataOutputStream(comm.getOutputStream());
                  dos.writeBytes(twoHyphens + boundary + lineEnd);
                  dos.writeBytes("Content-Disposition: form-data; name=\"fileName\";filename=\""
                          + fileName + "\"" + lineEnd);
                  dos.writeBytes(lineEnd);
                  bytesAvailable = fis.available();
                  TotalSize = bytesAvailable;
                  bufferSize = Math.min(bytesAvailable, maxBufferSize);
                  buffer = new byte[bufferSize];
  
                  bytesRead = fis.read(buffer, 0, bufferSize);
  
                  while (bytesRead &gt; 0)
  
                  {
                      dos.write(buffer, 0, bufferSize);
                      bytesAvailable = fis.available();
                      bufferSize = Math.min(bytesAvailable, maxBufferSize);
                      bytesRead = fis.read(buffer, 0, bufferSize);
                      publishProgress(bytesRead);
  
                  }
  
                  dos.writeBytes(lineEnd);
                  dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                  serverResonseCode = comm.getResponseCode();
  
                  if (serverResonseCode == 200) {
                      Logger.error("File Upload Completed.");
                  }
  
                  fis.close();
                  dos.flush();
                  dos.close();
              } catch (MalformedURLException e) {
                  e.printStackTrace();
                  Log.e("Upload file to server", "error: " + e.getMessage(),e);
              } catch (Exception e) {
                  e.printStackTrace();
                  Log.e("Upload file to server", "error: " + e.getMessage(),e);
              }
  
              return TotalSize;
          }
      }
  </code></pre>
  
  <p>Okey now i upload from website, it return place where file in server:</p>
  
  <blockquote>
    <p>FieldName=fileName</p>
    
    <p>FileName=epl-v10.html</p>
    
    <p>ContentType=text/html</p>
    
    <p>Size in bytes=16536</p>
    
    <p>Absolute Path at server=C:\Users\dungdv4\Desktop\Android My Code
    Stack\sv tomcat\tmpfiles\epl-v10.html</p>
  </blockquote>
  
  <p>But in eclipse run, it say :</p>
  
  <blockquote>
    <p>03-04 08:27:48.249: E/###########(2643): start</p>
    
    <p>03-04 08:27:48.249: E/###########(2643): path: 
    /data/data/com.###########.###########/databases/###########-db</p>
    
    <p>03-04 08:27:48.253: E/###########(2643): File Upload Completed. </p>
    
    <p>03-04 08:27:48.253: E/###########(2643):  See uploaded file here :
    /var/www/uploads</p>
    
    <p>03-04 08:27:48.261: E/###########(2643): result : null</p>
  </blockquote>
  
  <p>it said file upload successful, but i cant find this file in server </p>