1

Android でチャット アプリケーションを実装したいです。アプリケーションでサーバーにファイルをアップロードしたいので、サーバー側でファイル要求をリッスンするためにこのコードを使用しています。

 protected void processRequest(HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html;charset=UTF-8");
    // Create path components to save the file
    final String path = "/home/vibhor/vibhor";
    final Part filePart = request.getPart("file");
    final String fileName = getFileName(filePart);
    OutputStream out = null;
    InputStream filecontent = null;
    final PrintWriter writer = response.getWriter();
    try {
        out = new FileOutputStream(new File(path + File.separator
                + fileName));
        filecontent = filePart.getInputStream();

        int read = 0;
        final byte[] bytes = new byte[1024];

        while ((read = filecontent.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        writer.println("New file " + fileName + " created at " + path);
    } catch (FileNotFoundException fne) {
        writer.println("You either did not specify a file to upload or are "
                + "trying to upload a file to a protected or nonexistent "
                + "location.");
        writer.println("<br/> ERROR: " + fne.getMessage());

    } finally {
        if (out != null) {
            out.close();
        }
        if (filecontent != null) {
            filecontent.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
}

このような HTML フォームを使用してファイル要求を行うと、上記のコードが機能します

<form action="UploadServlet" method="post"
                    enctype="multipart/form-data">
 <input type="file" name="file" size="50" />
 <br />
 <input type="submit" value="Upload File" />
 </form>

したがって、クライアント側で以下のコードを使用して同じリクエストを行いたいです(Androidアプリケーションで)

public static void uploadFile(String selectedPath, String fileType,
        String fileName) {
    HttpURLConnection httpUrlConnection = null;
    DataOutputStream dataOutputStream = null;
    DataInputStream dataInputStream = null;
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    String urlString = "http://localhost:8080/FileUploadServlet1/UploadServlet";
    File file = new File(selectedPath);
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        URL url = new URL(urlString);
        httpUrlConnection = (HttpURLConnection) url.openConnection();
        httpUrlConnection.setDoInput(true);
        httpUrlConnection.setConnectTimeout(50000000);
        httpUrlConnection.setDoOutput(true);
        httpUrlConnection.setUseCaches(false);
        httpUrlConnection.setChunkedStreamingMode(maxBufferSize);
        httpUrlConnection.setRequestMethod("POST");
        httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
        httpUrlConnection.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);
        httpUrlConnection.setRequestProperty("filename", fileName);
        httpUrlConnection.setRequestProperty("filetype", fileType);

        dataOutputStream = new DataOutputStream(
                httpUrlConnection.getOutputStream());
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        int offset = 0;
        bytesRead = fileInputStream.read(buffer, offset, bufferSize);
        while (bytesRead > 0) {
            dataOutputStream.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }
        fileInputStream.close();
        dataOutputStream.flush();
        dataOutputStream.close();
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        dataInputStream = new DataInputStream(
                httpUrlConnection.getInputStream());
        dataInputStream.close();
    } catch (IOException ioex) {
        ioex.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

しかし、この場合、この行で例外が発生しています

最終文字列 fileName = getFileName(filePart);

私はApacheファイルアップロードライブラリも使用しましたが、その場合もこの行に値はありません

list fileItems = upload.parseRequest(request);

では、uploadFile() メソッドでどのような変更を行う必要がありますか? 助けてください。

4

1 に答える 1