gpt4 book ai didi

php - 使用多部分实体android在服务器上上传大文件

转载 作者:行者123 更新时间:2023-11-30 02:52:59 26 4
gpt4 key购买 nike

您好,我正在使用多部分实体在服务器上上传文件,但它只在 php 服务器上上传小文件现在我想使用多部分实体上传大文件,我该怎么做?

-我正在从 sdcard 中获取大文件

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;
}

最佳答案

*  # To upload the image from android mobile to server(php).Pass image path as a argument of uplaodImage() method and for backend use php script.That is in the last of Android code.*/





public void uploadImage(String filePath)
{
try
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Constants.url);
FileBody filebodyImage = new FileBody(new File(filePath));

StringBody title = new StringBody("Filename: " + filePath);

StringBody description = new StringBody(
"This is a description of the video");
MultipartEntity multipart=new MultipartEntity();
multipart.addPart("Image",filebodyImage);
multipart.addPart("title",title);
multipart.addPart("description", description);
httpPost.setEntity(multipart);
httpPost.setEntity(multipart);
System.out.println("Executing Request "+httpPost.getRequestLine());
HttpResponse httpResponse=httpClient.execute(httpPost);
HttpEntity httpEntity=httpResponse.getEntity();
System.out.println( httpResponse.getStatusLine( ) );
if (httpEntity != null)
{
System.out.println( EntityUtils.toString( httpEntity ) );
}
// end if
if (httpEntity != null) {
httpEntity.consumeContent( );
} // end if

httpClient.getConnectionManager( ).shutdown( );
System.out.println("Uplaod file Executed");
}
catch(ParseException e)
{
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}




# Php Script
file name=UplaodServer.php

<?php
$file_path = basename( $_FILES['Image']['name']) ;
if(move_uploaded_file($_FILES['Image']['tmp_name'], $file_path)) {
echo "success";
} else{
echo "fail";
}
?>

关于php - 使用多部分实体android在服务器上上传大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23820859/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com