0

我可以在不使用GZIPOutputStream服务器的情况下上传图像。但我有使用要求GZIPOutputStream。所以我用过:

public String multipartRequest(String urlTo, String post, String filepath, String filefield) throws ParseException, IOException {
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    InputStream inputStream = null;

    String twoHyphens = "--";
    String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
    String lineEnd = "\r\n";

    String result = "";

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;

    String[] q = filepath.split("/");
    int idx = q.length - 1;

    try {
        File file = new File(filepath);
        FileInputStream fileInputStream = new FileInputStream(file);

        // code my
        byte[] data = new byte[(int) file.length()];

        URL url = new URL(urlTo);
        connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        // connection.setRequestProperty("Content-Encoding", "gzip");

        outputStream = new DataOutputStream(connection.getOutputStream());

        // outputStream.writeBytes("HTTP/1.1 200 OK\r\n");
        // outputStream.writeBytes("Content-Type: application/x-gzip");

        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"" + filefield + "\"; filename=\"" + q[idx] + ".gz" + "\""
                + lineEnd);
        // outputStream.writeBytes("Content-Disposition: form-data; name=\"" + filefield + "\"; filename=\"" +
        // q[idx] + "\"" + lineEnd);
        outputStream.writeBytes("Content-Type: image/jpeg" + lineEnd);
        outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
        outputStream.writeBytes(lineEnd);

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

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

        outputStream.writeBytes(lineEnd);

        // Upload POST Data
        String[] posts = post.split("&");
        int max = posts.length;
        for (int i = 0; i < max; i++) {
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            String[] kv = posts[i].split("=");
            outputStream.writeBytes("Content-Disposition: form-data; name=\"" + kv[0] + ".gz" + "\"" + lineEnd);
            // outputStream.writeBytes("Content-Disposition: form-data; name=\"" + kv[0] + "\"" + lineEnd);
            outputStream.writeBytes("Content-Type: text/plain" + lineEnd);
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(kv[1]);
            outputStream.writeBytes(lineEnd);
        }
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // For GZip outputStream //

        try (GZIPOutputStream gzos = new GZIPOutputStream(outputStream)) {
            gzos.write(data);
            gzos.close();
            Log.v("GZip", "Working");
        } catch (IOException e) {
            Log.v("TraceError", e.getMessage().toString());
            e.printStackTrace();
        }

        // end
        inputStream = connection.getInputStream();
        result = this.convertStreamToString(inputStream);
        Log.v("MYRESULT", result);

        fileInputStream.close();
        inputStream.close();
        outputStream.flush();
        outputStream.close();
        return result;
    } catch (Exception e) {
        Log.e("MultipartRequest", "Multipart Form Upload Error");
        e.printStackTrace();
        return "error";
    }

}

private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

在这里我可以看到

Log.v("GZip", "Working");

表示文件已压缩,但图像未上传到服务器。

我看不到这句话意味着没有成功。我也没有收到任何类型的错误、警告等。

Log.v("MYRESULT", result);

那么任何人都可以有一个想法吗?高级帮助将不胜感激!

4

2 回答 2

0

最后我自己解决了。!

我在这里做错了!

outputStream.writeBytes("Content-Disposition: form-data; name=\"" + kv[0] + ".gz" + "\"" + lineEnd);

我正在附加".gz"到其他表单数据。我只需要附加文件名。

真是个傻子!

于 2016-08-11T05:01:55.013 回答
0

您没有将输入流正确读取到数据中。您在每个循环上都覆盖它。将代码更改为

int totalBytesRead = 0;
bytesRead = fileInputStream.read(data, 0, data.length); 
while (bytesRead > 0) { 
    totalBytesRead += bytesRead;  
    bytesRead = fileInputStream.read(data, totalBytesRead, data.length - totalBytesRead);
}
outputStream.write(data);
于 2016-08-11T04:58:41.663 回答