我用谷歌搜索了我的问题,但没有找到任何东西。我在win 7中创建了一个服务器来将文件从android手机上传到它。但我无法上传文件。我不知道服务器设置/配置是真的吗?![server1][1]
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String pathToOurFile = "/sdcard/test.txt"; //complete path of file from your android device
String urlServer = "http://www.rar.com/";// complete path of server
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
Log.i("*******urlServer**1**", "urlServer ");
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
try {
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));
Log.i("*******fileInputStream******", "sent: ");
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
Log.i("*******HttpURLConnection**1**", "v ");
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
Log.i("*******setRequestProperty**1**", "v ");
outputStream = new DataOutputStream(connection.getOutputStream());
Log.i("*******DataOutputStream******", "DataOutputStream: ");
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
Log.i("*******lineEnd******", "writeBytes: ");
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
Log.i("*******befwhile******", "byteread ");
Log.i("*******byteread******", String.valueOf(bytesRead));
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
Log.i("*******befwhile******", "end while ");
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
Log.i("*******befwhile******", "twoHyphens ");
// Responses from the server (code and message)
int serverResponseCode = 0;
String serverResponseMessage = "";
serverResponseCode = connection.getResponseCode();
Log.i("*******befwhile******", "getResponseCode1 ");
serverResponseMessage = connection.getResponseMessage();
Log.i("*******befwhile******", "getResponseMessage2 ");
fileInputStream.close();
outputStream.flush();
outputStream.close();
Log.i("*******Sending******", "sent: Final***");
}
catch (Exception ex) {
Log.i("*******exception&&" + ex.toString(), ex.getMessage());
//Exception handling
}
我也不知道 outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile + "\"" + lineEnd); 当我运行上面的代码时,它运行到 Log.i(" ** befwhile * ", "twoHyphens "); 请帮我!