java - GAE 上传文件 POST 请求(URLConnection) - 错误 : the uploaded file was only partially uploaded

标签 java google-app-engine google-cloud-storage

我尝试使用 GAE 将文件从 blobstore 上传到服务器(127.0.0.1),但接收服务器给我错误“上传的文件仅部分上传”。 我还在请求中发送了一个参数,服务器正确接收了该参数。

String url = "http://127.0.0.1/";
String charset = "UTF-8";

FileService fileService = FileServiceFactory.getFileService();
AppEngineFile appEngineFile = fileService.getBlobFile(new BlobKey("blob key"));

String param = "my param";
File binaryFile = new File(appEngineFile.getFullPath());
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
String CRLF = "\r\n"; // Line separator required by multipart/form-data.

URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
PrintWriter writer = null;
try {
    OutputStream output = connection.getOutputStream();
    writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // true = autoFlush, important!

    // Send normal param.
    writer.append("--" + boundary).append(CRLF);
    writer.append("Content-Disposition: form-data; name=\"api_password\"").append(CRLF);
    writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
    writer.append(CRLF);
    writer.append(param).append(CRLF).flush();

    // Send binary file.
    writer.append("--" + boundary).append(CRLF);
    writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
    writer.append("Content-Type: application/octet-stream").append(CRLF);
    writer.append("Content-Transfer-Encoding: binary").append(CRLF);
    writer.append(CRLF).flush();
    InputStream input = null;
    try {

    BlobInfoFactory blobInfoFactory = new BlobInfoFactory();
    BlobInfo blobInfo = blobInfoFactory.loadBlobInfo(new BlobKey("blob key"));
    Long blobSize = blobInfo.getSize();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    //max read on fetch
    long maxReadFetch = 1015807;

    //read the file in one time temporary
    long i = 0;
    long start = 0;
    long end = 0;
    while(i < blobSize) {

    start = i;
    end = i + maxReadFetch;
    //determine end
    if(end > blobSize) {
    end = blobSize;
    }
    buffer.write(blobstoreService.fetchData(new BlobKey("blob key"), start, end));
    i += maxReadFetch;

    }

    output.write(buffer.toByteArray());

        output.flush(); // Important! Output cannot be closed. Close of writer will close output as well.

    } finally {
        if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
    }
    writer.append(CRLF).flush(); // CRLF is important! It indicates end of binary boundary.

    // End of multipart/form-data.
    writer.append("--" + boundary + "--").append(CRLF);


    //resp part
InputStream responseStream = new BufferedInputStream(connection.getInputStream());

BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));

StringBuilder stringBuilder = new StringBuilder();
String line2;
while ((line2 = responseStreamReader.readLine()) != null)
{
    stringBuilder.append(line2).append("\n");
}
responseStreamReader.close();

String response = stringBuilder.toString();
resp.getWriter().write(response);
} finally {
    if (writer != null) writer.close();
}

最佳答案

由于取消上传导致错误“上传的文件仅部分上传”,我认为您的 GAE 请求在 writer.append("--"+ border 之前可能因 30 秒的执行限制或异常而中止+ "--").append(CRLF); 发生。也许输出日志语句来查看代码在哪里停止。

关于java - GAE 上传文件 POST 请求(URLConnection) - 错误 : the uploaded file was only partially uploaded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15095257/

相关文章:

java - Spring 中的 Java 通用语法是什么?

java - 在 Java 中使用注释跟踪脏属性?

java - GAE 存储到数据库但不显示在 View 上

java - ArrayList HashMap 未定义

google-cloud-storage - 从 GKE pod 内部进行 GCS 写访问

java - 绑定(bind)到操作/结果的方法中的 "allowed"是什么

java - ClassNotFoundException 即使包含该类的 jar 正确存在于类路径中

android - 如何从 google eclipse 插件 1.7.7 更改为 1.7.5

python - 使用Python将zip文件从Google云存储上传到SFTP服务器,而不加载到内存中

reactjs - Google Cloud Bucket 和 ReactJS 应用访问