1
私は別のサーバーにファイルをアップロードするためにhttpurlconnectionを使用しています。行を:java httpurlconnection "読み込みに失敗しました:行から列:"
いくつかのホストについては、次のコードが動作していると私はファイルをアップロードすることができるよ、私は別のホストにアップロードしようとしているとき、私は「エラーメッセージ
を取得するには、読み取りに失敗しました長すぎる:\ nContent-処分:?。。フォームデータ、名前...
このエラーは、私はGoogleとの任意のヒントを見つけることができませんでした何を意味していますが、役立つことを願っ:)
private void upload(){
response = "";
DataOutputStream outputStream = null;
InputStream inputStream = null;
String twoHyphens = "--";
String boundary = "--------"+Long.toString(System.currentTimeMillis());
String lineEnd = "\n";
try {
File file = getFile();
StringBuffer sb = new StringBuffer();
sb.append(twoHyphens + boundary + lineEnd);
sb.append("Content-Disposition: form-data; name=\"file1\"; filename=\"" + getFilename() +"\"" + lineEnd);
sb.append("Content-Type: " + getMime() + lineEnd);
sb.append("Content-Transfer-Encoding: binary" + lineEnd);
sb.append(lineEnd);
FileInputStream fileInputStream = new FileInputStream(file);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setFixedLengthStreamingMode((uploadContainer.getFilesize() + sb.length() + lineEnd.length() + twoHyphens.length() + boundary.length() + twoHyphens.length() + lineEnd.length()));
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("User-Agent", USER_AGENT);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"file1\"; filename=\"" + getFilename() +"\"" + lineEnd);
outputStream.writeBytes("Content-Type: " + getMime() + lineEnd);
outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
outputStream.writeBytes(lineEnd);
int bytesRead = -1;
byte[] buffer = new byte[4096];
boolean cancelled = false;
while ((bytesRead = fileInputStream.read(buffer)) > 0){
outputStream.write(buffer, 0, bytesRead);
if(Thread.currentThread().isInterrupted()){
cancelled = true;
break;
}
}
if(cancelled == false){
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
outputStream.flush();
inputStream = connection.getInputStream();
response = convertStreamToString(inputStream);
inputStream.close();
} else {
// ...
}
fileInputStream.close();
} catch(Exception e) {/e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
これは機能します!ありがとう。しかし、なぜそれはいくつかのホストと他のnotsと協力しているのですか?それは私を少し混乱させます。 – Ronon
いくつかのホストは '\ n'を受け入れ、いくつかは受け入れないので。 – EJP
ちょうど簡単です。あなたの迅速な助けをありがとう! – Ronon