1
嗨我上傳文件在服務器上使用多部分實體,但它只上傳php服務器上的小文件 現在我想上傳使用多部分實體的大文件,所以我怎麼做到這一點?上傳使用多部分實體的服務器上的大文件android
-I是從SD卡獲取大文件
public String uploadfile(String uploadFile, String crimedetails, String lat)
{
String url;
MultipartEntity entity;
try {
url = String.format(WSConstants.SERVER_URL
+ WSConstants.URL_SET_POST);
entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
//
File file = new File(uploadFile);
if (file.exists())
{
InputStream inputStream = null;
ByteArrayOutputStream bos = null;
try
{
inputStream = new FileInputStream(file);
bos = new ByteArrayOutputStream();
//byte[] b = new byte[1 * 1024 * 1024];
byte[] b = new byte[1024 * 8];
int bytesRead = 0;
while ((bytesRead = inputStream.read(b)) != -1)
{
System.gc();
bos.write(b, 0, bytesRead);
bos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
InputStream in = new ByteArrayInputStream(bos.toByteArray());
ContentBody foto = new InputStreamBody(in, "application/pdf",uploadFile);
entity.addPart("uploadfile", foto);
}
else {
FormBodyPart image = new FormBodyPart("uploadfile",
new StringBody(""));
entity.addPart(image);
}
FormBodyPart userId = new FormBodyPart("filename", new StringBody(
String.valueOf(crimedetails)));
entity.addPart(userId);
FormBodyPart crimeType = new FormBodyPart("filetime",
new StringBody(String.valueOf(lat)));
entity.addPart(crimeType);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
return "error";
}
HttpParams httpParams = new BasicHttpParams();
HttpContext httpContext = new BasicHttpContext();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
String result = null;
try {
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer sb = new StringBuffer();
String line = null;
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
result = sb.toString();
} finally {
if (in != null)
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
檢查該SO帖子,希望這將有助於ü.... HTTP://stackoverflow.com/questions/21511981/upload-a -file到一個-URL/21513158#21513158 – ajitksharma