想请问下android如何同时上传文件和键值对? HttpPost post = new HttpPost( "http://veikr.com/myupload/upload.php"); File file = new File("/mnt/sdcard/infor.txt"); MultipartEntity multipart = new MultipartEntity(); multipart.addPart("name", new StringBody("veikr.com")); multipart.addPart("file", new FileBody(file)); HttpClient client = new DefaultHttpClient(); post.setEntity(multipart); HttpResponse response = client.execute(post); return response; 我试过用上边的方式给服务器上传。但是服务器接收不到。 |
|
没人么… 自己顶一下…
|
|
没用过,帮顶一下吧
|
|
我试了没有问题啊。
你检查一下是不是因为文件不存在或者没有访问权限导致异常。 或者是服务器没有处理好? |
|
30分 |
如果文件不存在,服务器就会接收到不完整的数据。
|
文件存在的…但是服务器那边接受不到。好像这个方法服务器那边要额外用一个jar包是么?还是正常接收就行。 |
|
我的测试服务器程序不是用java实现的。这些数据都是通过http协议传送的,和服务端用什么语言实现没有关系。 |
|
20分 |
建议楼主是用afinal框架,他的post可以提交文件和表单数据。
这个是我改进的一个文件、表格数据提交post: package com.minephone.network; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; import android.os.AsyncTask; import android.util.Log; /** * 文件上传 * * HashMap<String, String> params = new HashMap<String, String>(); params.put("page", "1"); params.put("name", "2"); HashMap<String, File> files = new HashMap<String, File>(); files.put("1.apk","/mnt/sdcard/1.apk")); files.put("name", new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"6.jpg")); new fileupload("http://192.168.191.1:8081/uploadtest/servlet/upload2",params, files,MainActivity.this).execute(""); * @author ping * */ public class FileUpload extends AsyncTask { public static final String TAG = "FileUpload"; String actionUrl; Map<String, String> params; Map<String, File> files; UploadListener listener; public interface UploadListener { public void onUploadEnd(boolean success,String object); } public FileUpload(String actionUrl, Map<String, String> params,Map<String, File> files,UploadListener listener) { Log.i(TAG,"upload!"); this.actionUrl = actionUrl; this.params = params; this.files = files; this.listener = listener; } @Override protected String doInBackground(Object... arg0) { String reslut = null; try { String BOUNDARY = java.util.UUID.randomUUID().toString(); String PREFIX = "--", LINEND = "\r\n"; String MULTIPART_FROM_DATA = "multipart/form-data"; String CHARSET = "UTF-8"; URL uri = new URL(actionUrl); HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); conn.setReadTimeout(5 * 1000); conn.setDoInput(true);// 允许输入 conn.setDoOutput(true);// 允许输出 conn.setUseCaches(false); conn.setRequestMethod("POST"); // Post方式 conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY); // 首先组拼文本类型的参数 StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : params.entrySet()) { sb.append(PREFIX); sb.append(BOUNDARY); sb.append(LINEND); sb.append("Content-Disposition: form-data; name="" + entry.getKey() + """ + LINEND); sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND); sb.append("Content-Transfer-Encoding: 8bit" + LINEND); sb.append(LINEND); sb.append(entry.getValue()); sb.append(LINEND); } DataOutputStream outStream = new DataOutputStream(conn.getOutputStream()); outStream.write(sb.toString().getBytes()); // 发送文件数据 if (files != null) // for (Map.Entry<String, File> file : files.entrySet()) { for (String key : files.keySet()) { StringBuilder sb1 = new StringBuilder(); sb1.append(PREFIX); sb1.append(BOUNDARY); sb1.append(LINEND); sb1.append("Content-Disposition: form-data; name="file"; filename="" + key + """ + LINEND); sb1.append("Content-Type: multipart/form-data; charset=" + CHARSET + LINEND); sb1.append(LINEND); outStream.write(sb1.toString().getBytes()); File valuefile = files.get(key); InputStream is = new FileInputStream(valuefile); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { outStream.write(buffer, 0, len); } is.close(); outStream.write(LINEND.getBytes()); } // 请求结束标志 byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes(); outStream.write(end_data); outStream.flush(); // 得到响应码 // success = conn.getResponseCode()==200; InputStream in = conn.getInputStream(); InputStreamReader isReader = new InputStreamReader(in); BufferedReader bufReader = new BufferedReader(isReader); String line = null; reslut = ""; while ((line = bufReader.readLine()) != null) reslut += line; outStream.close(); conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } return reslut; } @Override protected void onPreExecute() { //onPreExecute方法用于在执行后台任务前做一些UI操作 Log.i(TAG, "onPreExecute() called"); } @Override protected void onCancelled() { //取消操作 Log.i(TAG, "onCancelled() called"); if (listener!=null) { listener.onUploadEnd(false, null); } } @Override protected void onPostExecute(Object result) { //onPostExecute方法用于在执行完后台任务后更新UI,显示结果 Log.i(TAG, "onPostExecute(Result result) called"); if (listener!=null) { if (result==null) { listener.onUploadEnd(false, null); } else { listener.onUploadEnd(true, result.toString()); } } } @Override protected void onProgressUpdate(Object... values) { //onProgressUpdate方法用于更新进度信息 Log.i(TAG, "onProgressUpdate() called"); } } |