当前位置: 代码迷 >> ASP.NET >> android Post文件到ASP.NET的有关问题,能收到参数收不到文件
  详细解决方案

android Post文件到ASP.NET的有关问题,能收到参数收不到文件

热度:11124   发布时间:2013-02-25 00:00:00.0
android Post文件到ASP.NET的问题,能收到参数收不到文件
我用POST方式从android发送参数和文件到ASP.NET做的服务器。代码如下:

VB.NET code
            '====Page_Load 的代码,我用数据库做标记,发现只能执行到102这里就停止了            Call_Insert.Insert_Logs(lx, "__101__")            Dim oFile As HttpPostedFile = Request.Files("file1")            Call_Insert.Insert_Logs(lx, "__102__")            Dim fs As Stream = oFile.InputStream            Call_Insert.Insert_Logs(lx, "__103__")            Dim by As Byte() = New Byte(oFile.InputStream.Length - 1) {}            Call_Insert.Insert_Logs(lx, "__104__")            '分块读取            Dim folderPath As String = Server.MapPath("~/UploadImages/")            Call_Insert.Insert_Logs(lx, "__105__")            Dim filePath As String = folderPath + oFile.FileName            Call_Insert.Insert_Logs(lx, "__106__")            If Not Directory.Exists(folderPath) Then                Call_Insert.Insert_Logs(lx, "__107__")                Directory.CreateDirectory(folderPath)            End If            Call_Insert.Insert_Logs(lx, "__108__")            Dim fStream As New FileStream(filePath, FileMode.Create)            Call_Insert.Insert_Logs(lx, "__109__")            Dim osize As Integer = fs.Read(by, 0, by.Length)            Call_Insert.Insert_Logs(lx, "__110__")            While osize > 0                If osize > 0 Then                    fStream.Write(by, 0, osize)                End If                osize = fs.Read(by, 0, by.Length)            End While            fStream.Close()



安卓的发送代码:

Java code
//POST String File_name ="JN_PIC_"+ formatter.format(curDate) + ".jpg";String actionUrl = "http://192.168.1.108:8012/get/get_msg.aspx";                         Map<String, String> params = new HashMap<String, String>();params.put("lx", "5");params.put("send_txt", (fileLen/1024)+"KB ("+DC_INF+")");params.put("send_txt2", DC_path); //DC_path是图片文件的路径,确定无误params.put("send_file_name", File_name);Map<String, File> files = new HashMap<String, File>();files.put("file1", new File(DC_path));try {Show_Toast(PostFile.post(actionUrl, params, files));}catch(Exception e){Show_Toast("失败");}fis.close();Show_Toast("完成");


Java code
    public static String post(String actionUrl, Map<String, String> params, Map<String, File> files) throws IOException {     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");     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()) {     StringBuilder sb1 = new StringBuilder();     sb1.append(PREFIX);     sb1.append(BOUNDARY);     sb1.append(LINEND);     sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\""+file.getKey()+"\""+LINEND);         sb1.append("Content-Type: application/octet-stream; charset="+CHARSET+LINEND);         sb1.append(LINEND);         outStream.write(sb1.toString().getBytes());     InputStream is = new FileInputStream(file.getValue());         byte[] buffer = new byte[1024000];     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();     // 得到响应码     int res = conn.getResponseCode();     if (res == 200) {         InputStream in = conn.getInputStream();     int ch;     StringBuilder sb2 = new StringBuilder();     while ((ch = in.read()) != -1) {     sb2.append((char) ch);     }     }         outStream.close();     conn.disconnect();     return "ok";     }
  相关解决方案