我正在尝试将视频文件从Android设备上传到http服务器。我使用了多部分文件上传。与音频文件一起使用时,代码工作正常。但是当我尝试上传视频文件时,我收到了HttpHostConnectException。我在应用中添加了所有必要的权限。
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest=new HttpPost("http://xxx.xxx.x.xxx:8090/WebAppTest/rest/file/upload?name=five.mp4");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody fb = new FileBody(filename, "video/3gpp");
FormBodyPart bodyPart = new FormBodyPart("file",fb);
reqEntity.addPart(bodyPart);
postRequest.setEntity(reqEntity);
HttpResponse response=httpClient.execute(postRequest);
答案 0 :(得分:1)
公共类BigFileUpload {
public static String TAG = "filevideoupload";
private static final char PARAMETER_DELIMITER = '&';
private static final char PARAMETER_EQUALS_CHAR = '=';
public String sendFileToServer(String filename, String targetUrl, String videoName)
{
String response = "error";
String postParameters;
Log.e(TAG, filename);
Log.e(TAG, targetUrl);
long start = System.currentTimeMillis();
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
// DataInputStream inputStream = null;
String pathToOurFile = filename;
String urlServer = targetUrl;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
// DateFormat df = new SimpleDateFormat("yyyy_MM_dd_HH:mm:ss");
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024;
try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setChunkedStreamingMode(1024);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
String connstr = null;
connstr = "Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + videoName + "\"" + lineEnd;
Log.i(TAG, "connstr->" + connstr);
outputStream.writeBytes(connstr);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
Log.e(TAG, "bytesAvailable " + bytesAvailable + "");
try
{
while (bytesRead > 0)
{
try
{
outputStream.write(buffer, 0, bufferSize);
}
catch (OutOfMemoryError e)
{
e.printStackTrace();
response = "outofmemoryerror";
Log.e(TAG, "115 OOM ");
return response;
}
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
}
catch (Exception e)
{
Log.e(TAG, "bytesAvailable error in try 124" + bytesAvailable + "");
e.printStackTrace();
response = "error";
return response;
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
Log.e(TAG, "112 --> end of wrte");
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
Log.i(TAG, "server response code " + serverResponseCode);
Log.i(TAG, "server response msg " + connection.getResponseMessage());
if (serverResponseCode == 200)
{
response = "true";
}
InputStream is;
if (connection.getResponseCode() != 200)
{
is = connection.getErrorStream();
}
else
{
is = connection.getInputStream();
}
// s.useDelimiter("\\Z");
Log.i(TAG, "server response content " + getStringFromInputStream(is));
fileInputStream.close();
outputStream.flush();
outputStream.close();
outputStream = null;
}
catch (Exception ex)
{
// Exception handling
response = "error";
Log.e(TAG, ex.getMessage() + "");
ex.printStackTrace();
}
Log.e(TAG, "Time for uplaod " + (System.currentTimeMillis() - start) / 1000 + " sekund:)");
return response;
}
private String getStringFromInputStream(InputStream is)
{
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try
{
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null)
{
sb.append(line);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return sb.toString();
}
}
使用此类进行文件上传。我将它用于视频上传。
它确实有用