java 表单上传

    xiaoxiao2021-03-25  86

    String end = "\r\n";         String twoHyphens = "--";         String boundary = "-------------------------7de16713908a8";         try         {             URL url =new URL(actionUrl);             HttpURLConnection con=(HttpURLConnection)url.openConnection();             /* 允许Input、Output,不使用Cache */             con.setDoInput(true);             con.setDoOutput(true);             con.setUseCaches(false);             /* 设置传送的method=POST */             con.setRequestMethod("POST");             /* setRequestProperty */                          con.setRequestProperty("Connection", "Keep-Alive");             con.setRequestProperty("Charset", "UTF-8");             con.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary);             con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");             con.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");                          con.setRequestProperty("Accept-Encoding", "gzip,deflate");             con.setRequestProperty("Accept-Language", "zh-CN");             con.setRequestProperty("Cache-Control", "no-cache"); //            con.setRequestProperty("uptype", "client");                          String fileType = getFileType(uploadFile);                          String newName = getFileName(uploadFile);             String content_disposition = "Content-Disposition: form-data; " +                     "name=\"upfile\"; filename=\"" +                     newName +".text" +"\"" + end;             String content_type = "Content-Type: text/plain"+end;                          String content_length = ""+ getFileLength(uploadFile.getAbsolutePath()) + content_disposition.getBytes().length+ content_type.getBytes().length;              //            con.setRequestProperty("Content-Length", content_length);              //            con.setRequestProperty("Cookie", cookie);             /* 设置DataOutputStream */             DataOutputStream ds = new DataOutputStream(con.getOutputStream());                          ds.writeBytes(twoHyphens + boundary + end);//-------                                       ds.writeBytes(content_disposition); //            LogUtils.e(content_disposition);                          ds.writeBytes(content_type);             ds.writeBytes(end);                //---- 内容 -----                            /* 取得文件的FileInputStream */             FileInputStream fStream = new FileInputStream(uploadFile);             /* 设置每次写入1024bytes */             int bufferSize = 1024;             byte[] buffer = new byte[bufferSize];                          int length = -1;             /* 从文件读取数据至缓冲区 */             while((length = fStream.read(buffer)) != -1)             {                 /* 将资料写入DataOutputStream中 */                 ds.write(buffer, 0, length);             }             //----- 结束标记--             ds.writeBytes(end);             ds.writeBytes(twoHyphens + boundary+"--" + end);                          /* close streams */             fStream.close();             ds.flush();                          /* 取得Response内容 */ //        String responseMessage = con.getResponseMessage();             InputStream is = con.getInputStream();             StringBuffer b =new StringBuffer();             java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(is , "gbk"));             String str = null;             while((str= breader.readLine()) != null){ //                LogUtil.e("-----"+str);                 b.append(str); //            str = breader.readLine();             }             Object content = con.getContent();             /* 将Response显示于Dialog */             LogUtil.e("上传成功"); //        showDialog("上传成功",context);             /* 关闭DataOutputStream */             ds.close();             String strFromServer = b.toString().trim();
    转载请注明原文地址: https://ju.6miu.com/read-13792.html

    最新回复(0)