gpt4 book ai didi

java - 从 Android 上传 Google App Engine Java Blobstore 无响应

转载 作者:行者123 更新时间:2023-11-30 02:59:45 24 4
gpt4 key购买 nike

我正在尝试从 Android 将图像上传到 blobstore,但是当我的服务器 servlet 在完成时被调用时,Blobstore 无法传递任何 BlobKey。当我检查实际的 blobstore 时,没有上传任何图像。没有错误代码,没有警告,没有错误,什么都没有。现在我知道服务器的东西是有效的,因为上传在 iOS 中有效。 Android 代码在本地开发服务器上运行良好,但在生产环境中却无法运行。我希望也许有人可以发现我的代码中的错误。

提前致谢。

HttpURLConnection connection = null;

try {

Log.d(LOG_TAG, "Starting multipart POST request to: " +fullUrlValue);

URL fullUrl = new URL(fullUrlValue);

//make server call
connection = getClient().getConnection(fullUrl, RequestMethod.POST);
connection.setConnectTimeout(60000); //60 seconds to complete an upload
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);

//see if we are streaming the upload
if(requestOptions == null || requestOptions.isChunckedStreamingMode())
{
connection.setChunkedStreamingMode(2048);
}

String boundry = "z6fQbdm2TTgLwPQj9u1HjAM25z9AJuGSx7WG9dnD";

connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Cache-Control", "no-cache");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundry);

//attach post objects
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());

//add form fields
if(additionalParameters != null && additionalParameters.keySet().size() > 0)
{
Iterator<String> it = additionalParameters.keySet().iterator();
while(it.hasNext())
{
String fieldName = it.next();
if(fieldName != null)
{
String value = additionalParameters.get(fieldName);
if(value != null)
{
//add form field to upload form
outputStream.writeBytes("--" + boundry + "\r\n" + "Content-Disposition: form-data; name=\"" + fieldName + "\"\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\n"+value+"\r\n");
}
}
}
}

//attach file
if(postObjectBytes != null)
{
//build the request body
outputStream.writeBytes("--" + boundry + "\r\n" + "Content-Disposition: form-data; name=\"" + formElementName + "\";filename=\"" + fileName + "\"\r\n\r\n");


//object upload content size
long totalUploadSize = postObjectBytes.length;

//we have to manually process the stream in order to correctly update the progress listener
byte[] buffer = new byte[2048];
long totalBytes = 0;
int bytesRead = 0;

InputStream inputStream = new ByteArrayInputStream(postObjectBytes);

while ((bytesRead = inputStream.read(buffer)) > -1) {
totalBytes += bytesRead;
outputStream.write(buffer, 0, bytesRead);

if(progressListener != null){
progressListener.transferred(totalBytes, totalUploadSize);
}
}

outputStream.writeBytes("\r\n--" + boundry + "--\r\n");

}

outputStream.flush();
outputStream.close();

最佳答案

好吧,感谢AppEngine BlobStore upload failing with a request that works in the Development Environment,我找到了答案.

显然我在分号和“文件名”的开头之间缺少一个空格。那太疯狂了。

formElementName + "\";filename

关于java - 从 Android 上传 Google App Engine Java Blobstore 无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22727041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com