Java 仅从android通过特定网络上传图像失败
我们正在从android设备上传图像到我们的服务器。在使用移动数据和Wi-Fi的同时,图像已成功上载到服务器,但我们当前使用的一个特定网络除外。这种新的网络设置不允许从MTU大小大于1460的客户端传递任何数据。因此,来自android设备的所有数据包都被拒绝,无法到达服务器。我们的iOS应用程序可以通过相同的网络将图像成功上传到相同的服务器。我们通过跟踪发现,如果一个数据包未成功发送,iOS会减少数据包或数据块大小。但安卓设备的表现并非如此。我们还发现,一些社交网络android应用程序可以成功地通过我们的网络上传图像。有些人可能会说用正确的设置来改变我们的网络,但我们不想这样做,就好像我们可以拥有一个有缺陷的网络,其他人也可以拥有这样的网络,而我们的android应用程序将无法正常工作 到目前为止,我们已经尝试的工作(除新网络外,所有工作都在进行中)如下所示: 我们从一开始使用的主要代码:Java 仅从android通过特定网络上传图像失败,java,android,networking,image-uploading,Java,Android,Networking,Image Uploading,我们正在从android设备上传图像到我们的服务器。在使用移动数据和Wi-Fi的同时,图像已成功上载到服务器,但我们当前使用的一个特定网络除外。这种新的网络设置不允许从MTU大小大于1460的客户端传递任何数据。因此,来自android设备的所有数据包都被拒绝,无法到达服务器。我们的iOS应用程序可以通过相同的网络将图像成功上传到相同的服务器。我们通过跟踪发现,如果一个数据包未成功发送,iOS会减少数据包或数据块大小。但安卓设备的表现并非如此。我们还发现,一些社交网络android应用程序可以成
private boolean uploadImage(String fileLoc, String title){
try {
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
URL connectURL;
connectURL = new URL(baseUrlString); // our server url to upload image
FileInputStream fileInputStream = new FileInputStream(fileLoc);
// ------------------ CLIENT REQUEST
// Open a HTTP connection to the URL
HttpURLConnection conn = (HttpURLConnection) connectURL
.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
//conn.setChunkedStreamingMode(512);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
//conn.setFixedLengthStreamingMode(137931);
DataOutputStream dos = new DataOutputStream(
conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\""
+ param1 + "\"" + lineEnd
+ lineEnd
+ title
+ lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\""
+ fileLoc
+ "\""
+ lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
fileInputStream.close();
dos.flush();
InputStream is = conn.getInputStream();
// retrieve the response from server
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
String response;
response = b.toString().trim();
dos.close();
// here some work with response
return sucs;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
在这里,我们还尝试了
conn.setChunkedStreamingMode(512);
conn.setRequestProperty("Transfer-Encoding", "chunked");
conn.setFixedLengthStreamingMode(calculatedValueOfbytesForSameImage);
限制块大小,但它被忽略,因为文档中说它只是一个提示
我们也尝试过使用
conn.setChunkedStreamingMode(512);
conn.setRequestProperty("Transfer-Encoding", "chunked");
conn.setFixedLengthStreamingMode(calculatedValueOfbytesForSameImage);
但失败了
然后,我们尝试使用Multipart和Socket实现web中的其他方法:
除了我们的网络,所有方法都有效。还尝试将Multipart与ByteArrayOutputStream组合为。但仍然不起作用
是否有其他方法可以使用其他类(如InterfaceAddress或本机支持)来控制块大小,或者如果我们在上面尝试的方法中缺少某些内容?谢谢