1
Dosyaları farklı sunuculara yüklemek için httpurlconnection kullanıyorum.java httpurlconnection "okunamadı: satır uzun:"
bazı ana bilgisayarlar için aşağıdaki kod çalışıyor ve ben dosya yüklemek mümkün değilim, ama başka ana bilgisayara yüklemeye çalışıyorum, ben bir hata mesaj
demek ben google ile herhangi bir ipucu bulamadı yardımcı Umut :)olsun "okunamadı: çizgi çok uzun: \ nContent-Disposition: form-data; adı ... Bu hata ne
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();
}
}
}
..
Çalışıyor! Teşekkürler. Ama neden bazı ana bilgisayarlarda ve diğer notlarla çalışıyor? Bu beni biraz karıştırıyor. – Ronon
Bazı ana bilgisayarlar \ n'yi kabul ediyor ve bazıları almıyor. – EJP
Sadece bu kadar kolay. Hızlı yardımınız için teşekkürler! – Ronon