日期:2014-05-17  浏览次数:20589 次

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.entr