Подтвердить что ты не робот

Не удалось загрузить файл на сервере tomcat через сервлет-код

Я пытаюсь загрузить файл на сервер Tomcat через сервлет, но безуспешно. Я могу создать каталог, но каким-то образом код сервлета не создает файл внутри каталога "загрузить".

Java-код в 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);
     }
}

Код сервлета на сервере Tomcat:

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 content to extract file data
        List formItems = upload.parseRequest(request);
        Iterator iter = formItems.iterator();

        // iterates over form 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);
 }
}

Моя файловая структура на сервере:

/webapps/
   demo/
      upload/ - The file I'm trying to create (test123.txt) should be inside this folder, but it not.
   WEB-INF/
      classes/ - UploadServlet.class
      lib/ - commons IO and upload jars
4b9b3361

Ответ 1

Твой заголовок Content-Disposition неверен. Он говорит post-data, пока он должен быть form-data в случае multipart/form-data. Исправьте его в своем клиенте:

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

См. также:


Несвязанный к конкретной проблеме, сохранение загруженных файлов в папке развертывания webapp - абсолютно плохая идея. Они будут потеряны всякий раз, когда вы повторно развертываете новую версию webapp, по той простой причине, что все загруженные ранее файлы наверняка не содержатся в исходной WAR. Храните их где-то еще вне папки развертывания. Просто никогда не используйте getRealPath(), это приведет только к плохой практике.

Кроме того, FileInputStream#available() абсолютно не делает то, что вы считали там в своем коде. Избавьтесь от него и просто выполните обычную запись-запись в цикле for с фиксированным размером буфера, например. 10KB.

Кроме того, использование DataOutputStream здесь страшно. Он выполняет другую задачу (создание файлов .dat), и в этой конкретной конструкции существует большой риск для связанных с кодировкой символов проблем. Просто используйте PrintWriter, который вы оберните вокруг OutputStreamWriter указанной кодировкой. Обратите внимание, что полный пример показан в приведенной выше ссылке "См. Также".