0
A
回答
0
有很多職位有關計算器多,試試這個
public static String uploadMultipartFile(String root, String token,
String username, String sourceFileUri,
String fileStyle)
{
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String pathToOurFile = sourceFileUri;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
StringBuffer response = new StringBuffer();
try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));
URL url = new URL(root);
connection = (HttpURLConnection) url.openConnection();
if (token != null)
{
connection.setRequestProperty("Authorization", "Basic " + token);
}
if (username != null)
{
connection.setRequestProperty("Username", username);
}
if (fileStyle != null)
{
connection.setRequestProperty("file-type", fileStyle);
}
String fileExtension = FilenameUtils.getExtension(sourceFileUri);
String mime = Utils.getFileMIME(fileExtension);
Log.d("uploadMultipartFile","fileExtension:"+fileExtension+",mime:"+mime);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Cache-Control", "no-cache");
connection.setRequestProperty("User-Agent", "CodeJava 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=\"uploadedfile\";filename=\""
+ pathToOurFile + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: "+ mime + lineEnd);
outputStream.writeBytes(lineEnd);
byte[]bytes = IOUtils.toByteArray(fileInputStream);
outputStream.write(bytes);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.i(HttpUtil.class.getSimpleName(), String.valueOf(serverResponseCode));
Log.i(HttpUtil.class.getSimpleName(), serverResponseMessage);
fileInputStream.close();
outputStream.flush();
outputStream.close();
BufferedReader br=null;
if(connection.getResponseCode()>=200 && connection.getResponseCode()<300)
{
br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
}
else if(connection.getResponseCode()>=400)
{
br = new BufferedReader(new InputStreamReader((connection.getErrorStream())));
}
String inputLine;
while ((inputLine = br.readLine()) != null) {
response.append(inputLine);
}
System.out.println("result from server:"+response.toString());
}
catch (Exception ex)
{
ex.printStackTrace();
}
return response.toString();
}
0
您需要將您的JPG文件轉換爲位圖對象,然後u需要將其壓縮成一個字節數組發送多請求。
public class FileUpload extends Activity {
Bitmap bm;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
// bm = BitmapFactory.decodeResource(getResources(),
// R.drawable.forest);
bm = BitmapFactory.decodeFile("/sdcard/DCIM/yourImage.jpg");
executeMultipartPost();
} catch (Exception e) {
Log.e(e.getClass().getName(), e.getMessage());
}
}
public void executeMultipartPost() throws Exception {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(
"http://10.0.2.2/cfc/iphoneWebservice.cfc?returnformat=json&method=testUpload");
ByteArrayBody bab = new ByteArrayBody(data, "yourImage.jpg");
// File file= new File("/mnt/sdcard/yourImage.jpg");
// FileBody bin = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uploaded", bab);
reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
System.out.println("Response: " + s);
} catch (Exception e) {
// handle exception here
Log.e(e.getClass().getName(), e.getMessage());
}
}
}
0
添加到您的gradle這個建立
compile('org.apache.httpcomponents:httpmime:4.3.6') {
exclude module: 'httpclient'
}
在下面
提到WebService類一樣編寫代碼public MyPojo post(String name, String gender, String celbID) {
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.addPart("Name", new StringBody(name, ContentType.TEXT_PLAIN));
multipartEntity.addPart("gender", new StringBody(gender, ContentType.TEXT_PLAIN));
multipartEntity.addPart("Celb_id", new StringBody(celbID, ContentType.TEXT_PLAIN));
httppost.setEntity(multipartEntity.build());
HttpResponse httpResponse = httpclient.execute(httppost);
HttpEntity httpEntity = httpResponse.getEntity();
aJsonResponse = EntityUtils.toString(httpEntity);
一旦你對此有疑問,請ping我...
+0
感謝您的信息:) – Olid
+0
@Olid,這是添加表單數據最簡單的方法。 –
相關問題
- 1. Python - 多部分表單發佈數據
- 2. 如何將多部分表單數據從jsp發送到Web服務?
- 3. 如何將多部分/表單數據從Angular發佈到Nodejs Multer?
- 4. 如何使用Power Query的Web發佈多部分/表單數據。目錄
- 5. ASP.NET如何在Web API中讀取多部分表單數據?
- 6. 如何從android發佈表單數據?
- 7. 如何發佈包含符號的多部分/表單數據
- 8. 將多部分表單發佈到外部URL php
- 9. 通過Android發佈多部分表單數據AsyncTask
- 10. 將表單數據發佈到服務器Api(Android)
- 11. 將表單與多個文件一起發佈到Web API
- 12. 將多部分表單數據發佈到seam + RESTeasy無法封送到InputStream
- 13. IE8發佈表單數據部分
- 14. 從Web客戶端流發佈多部分/表格數據文件
- 15. 如何將數組數據從表單發佈到MySQL DB
- 16. 將多個對象發佈到Web API
- 17. 如何將長數據發佈到Web API
- 18. 如何使用HttpClient將JSON數據發佈到Web API
- 19. 如何發佈multipart表單數據到asp.net web api動作列表
- 20. 如何從textarea表單發佈數據?
- 21. 測試ASP.NET Web API多部分表單數據文件上傳
- 22. 如何從HttpClient將數組發佈到web api控制器?
- 23. 發佈表單數據和文件到ASP.NET Web API
- 24. 將文件從MVC發佈到WEB API
- 25. 如何將數據從Flash表單發佈到PHP?
- 26. 如何將數據從網頁表單發佈到HTTPHandler.ashx文件?
- 27. 將BitmapImage發佈到Web API
- 28. 如何從Web表單獲取輸出並將其發佈到XML API?
- 29. 如何將請求數據發佈到Android中的Web服務?
- 30. aspnet web api如何驗證在多部分表單數據中使用modelstate?
使用改進,併發送多部分請求 –