打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用HttpUrlConnection进行post请求上传文件

使用HttpUrlConnection模拟post表单进行文件上传平时很少使用,比较麻烦。

 

原理是: 分析文件上传的数据格式,然后根据格式构造相应的发送给服务器的字符串。

格式如下:这里的httppost123是我自己构造的字符串,可以是其他任何的字符串

----------httppost123 (\r\n)
Content-Disposition: form-data; name="img"; filename="t.txt" (\r\n)
Content-Type: application/octet-stream (\r\n)

(\r\n)

sdfsdfsdfsdfsdf (\r\n)
----------httppost123 (\r\n)
Content-Disposition: form-data; name="text" (\r\n)

(\r\n)

text tttt (\r\n)
----------httppost123-- (\r\n)
(\r\n)

 

上面的(\r\n)表示各个数据必须以(\r\n)结尾。

 

具体Java代码如下:

 

 

Java代码  
  1. import java.io.ByteArrayOutputStream;  
  2. import java.io.DataOutputStream;  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.InputStream;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.SocketTimeoutException;  
  8. import java.net.URL;  
  9. import java.net.URLEncoder;  
  10. import java.util.HashMap;  
  11. import java.util.Iterator;  
  12. import java.util.Map;  
  13. import java.util.Set;  
  14.   
  15. import javax.imageio.ImageIO;  
  16. import javax.imageio.ImageReader;  
  17. import javax.imageio.stream.ImageInputStream;  
  18.   
  19. public class HttpPostUtil {  
  20.     URL url;  
  21.     HttpURLConnection conn;  
  22.     String boundary = "--------httppost123";  
  23.     Map<String, String> textParams = new HashMap<String, String>();  
  24.     Map<String, File> fileparams = new HashMap<String, File>();  
  25.     DataOutputStream ds;  
  26.   
  27.     public HttpPostUtil(String url) throws Exception {  
  28.         this.url = new URL(url);  
  29.     }  
  30.     //重新设置要请求的服务器地址,即上传文件的地址。  
  31.     public void setUrl(String url) throws Exception {  
  32.         this.url = new URL(url);  
  33.     }  
  34.     //增加一个普通字符串数据到form表单数据中  
  35.     public void addTextParameter(String name, String value) {  
  36.         textParams.put(name, value);  
  37.     }  
  38.     //增加一个文件到form表单数据中  
  39.     public void addFileParameter(String name, File value) {  
  40.         fileparams.put(name, value);  
  41.     }  
  42.     // 清空所有已添加的form表单数据  
  43.     public void clearAllParameters() {  
  44.         textParams.clear();  
  45.         fileparams.clear();  
  46.     }  
  47.     // 发送数据到服务器,返回一个字节包含服务器的返回结果的数组  
  48.     public byte[] send() throws Exception {  
  49.         initConnection();  
  50.         try {  
  51.             conn.connect();  
  52.         } catch (SocketTimeoutException e) {  
  53.             // something  
  54.             throw new RuntimeException();  
  55.         }  
  56.         ds = new DataOutputStream(conn.getOutputStream());  
  57.         writeFileParams();  
  58.         writeStringParams();  
  59.         paramsEnd();  
  60.         InputStream in = conn.getInputStream();  
  61.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  62.         int b;  
  63.         while ((b = in.read()) != -1) {  
  64.             out.write(b);  
  65.         }  
  66.         conn.disconnect();  
  67.         return out.toByteArray();  
  68.     }  
  69.     //文件上传的connection的一些必须设置  
  70.     private void initConnection() throws Exception {  
  71.         conn = (HttpURLConnection) this.url.openConnection();  
  72.         conn.setDoOutput(true);  
  73.         conn.setUseCaches(false);  
  74.         conn.setConnectTimeout(10000); //连接超时为10秒  
  75.         conn.setRequestMethod("POST");  
  76.         conn.setRequestProperty("Content-Type",  
  77.                 "multipart/form-data; boundary=" + boundary);  
  78.     }  
  79.     //普通字符串数据  
  80.     private void writeStringParams() throws Exception {  
  81.         Set<String> keySet = textParams.keySet();  
  82.         for (Iterator<String> it = keySet.iterator(); it.hasNext();) {  
  83.             String name = it.next();  
  84.             String value = textParams.get(name);  
  85.             ds.writeBytes("--" + boundary + "\r\n");  
  86.             ds.writeBytes("Content-Disposition: form-data; name=\"" + name  
  87.                     + "\"\r\n");  
  88.             ds.writeBytes("\r\n");  
  89.             ds.writeBytes(encode(value) + "\r\n");  
  90.         }  
  91.     }  
  92.     //文件数据  
  93.     private void writeFileParams() throws Exception {  
  94.         Set<String> keySet = fileparams.keySet();  
  95.         for (Iterator<String> it = keySet.iterator(); it.hasNext();) {  
  96.             String name = it.next();  
  97.             File value = fileparams.get(name);  
  98.             ds.writeBytes("--" + boundary + "\r\n");  
  99.             ds.writeBytes("Content-Disposition: form-data; name=\"" + name  
  100.                     + "\"; filename=\"" + encode(value.getName()) + "\"\r\n");  
  101.             ds.writeBytes("Content-Type: " + getContentType(value) + "\r\n");  
  102.             ds.writeBytes("\r\n");  
  103.             ds.write(getBytes(value));  
  104.             ds.writeBytes("\r\n");  
  105.         }  
  106.     }  
  107.     //获取文件的上传类型,图片格式为image/png,image/jpg等。非图片为application/octet-stream  
  108.     private String getContentType(File f) throws Exception {  
  109.           
  110. //      return "application/octet-stream";  // 此行不再细分是否为图片,全部作为application/octet-stream 类型  
  111.         ImageInputStream imagein = ImageIO.createImageInputStream(f);  
  112.         if (imagein == null) {  
  113.             return "application/octet-stream";  
  114.         }  
  115.         Iterator<ImageReader> it = ImageIO.getImageReaders(imagein);  
  116.         if (!it.hasNext()) {  
  117.             imagein.close();  
  118.             return "application/octet-stream";  
  119.         }  
  120.         imagein.close();  
  121.         return "image/" + it.next().getFormatName().toLowerCase();//将FormatName返回的值转换成小写,默认为大写  
  122.   
  123.     }  
  124.     //把文件转换成字节数组  
  125.     private byte[] getBytes(File f) throws Exception {  
  126.         FileInputStream in = new FileInputStream(f);  
  127.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  128.         byte[] b = new byte[1024];  
  129.         int n;  
  130.         while ((n = in.read(b)) != -1) {  
  131.             out.write(b, 0, n);  
  132.         }  
  133.         in.close();  
  134.         return out.toByteArray();  
  135.     }  
  136.     //添加结尾数据  
  137.     private void paramsEnd() throws Exception {  
  138.         ds.writeBytes("--" + boundary + "--" + "\r\n");  
  139.         ds.writeBytes("\r\n");  
  140.     }  
  141.     // 对包含中文的字符串进行转码,此为UTF-8。服务器那边要进行一次解码  
  142.     private String encode(String value) throws Exception{  
  143.         return URLEncoder.encode(value, "UTF-8");  
  144.     }  
  145.     public static void main(String[] args) throws Exception {  
  146.         HttpPostUtil u = new HttpPostUtil("http://localhost:3000/up_load");  
  147.         u.addFileParameter("img", new File(  
  148.                 "D:\\素材\\圆月.jpg"));  
  149.         u.addTextParameter("text", "中文");  
  150.         byte[] b = u.send();  
  151.         String result = new String(b);  
  152.         System.out.println(result);  
  153.   
  154.     }  
  155.   
  156. }  

 

 

后台使用ruby进行接收

ruby代码如下:

Ruby代码  
  1. require "cgi"  
  2. class UpLoadController < ApplicationController  
  3.     protect_from_forgery :except=>:index  
  4.       
  5.     def index  
  6.         img = params[:img]  
  7.         if img.kind_of? String  
  8.             logger.debug "img string : #{img}"  
  9.         else  
  10.             logger.debug "Content-Type:#{img.content_type}"  
  11.             logger.debug "or:#{CGI.unescape(img.original_filename)}"  
  12.         end  
  13.         text = params[:text]  
  14.         logger.debug "text:#{CGI.unescape(text)}"  
  15.         render :text=>"OK"  
  16.     end  
  17. end  

 

日志输入如下:

 Content-Type:image/jpeg
or:圆月.jpg
text:中文

 

如果不把中文转成UTF-8的格式进行传输,则后台显示中文乱码。

同样,如果其他参数包含中文,则也应当先转码。

当然,具体什么编码要和后台接收的编码一致。

 

 

 另外附上c#的代码,因为对c#不太熟悉,但代码测试基本可以实现了。

C#代码  
  1. using System;  
  2. using System.Drawing;  
  3. using System.IO;  
  4. using System.Net;  
  5. using System.Text;  
  6. using System.Drawing.Imaging;  
  7.   
  8. namespace MyThreading  
  9. {  
  10.     class HttpPostUtil  
  11.     {  
  12.         string boundary = "------httpost123";  
  13.         string url;  
  14.         byte[] bs= new byte[0];  
  15.         Encoding encoder = Encoding.UTF8;  
  16.         public HttpPostUtil(string url)  
  17.         {  
  18.             this.url = url;  
  19.         }  
  20.         public byte[] Send()  
  21.         {  
  22.             WebClient myWebClient = new WebClient();  
  23.             myWebClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);  
  24.             EndData();  
  25.             return myWebClient.UploadData(url,bs);   
  26.         }  
  27.         public void ClearData()  
  28.         {  
  29.             Array.Clear(bs, 0, bs.Length);  
  30.         }  
  31.         public void AddTextParameter(string name, string value)  
  32.         {  
  33.             StringBuilder s = new StringBuilder();  
  34.             s.Append("--").Append(boundary).Append("\r\n");  
  35.             s.Append("Content-Disposition:  form-data;  name=\"" + name + "\"\r\n");  
  36.             s.Append("\r\n");  
  37.             s.Append(value).Append("\r\n");  
  38.             AppendString(s.ToString());  
  39.         }  
  40.         public void AddFileParameter(string name, FileInfo file)  
  41.         {  
  42.             StringBuilder s = new StringBuilder();  
  43.             s.Append("--").Append(boundary).Append("\r\n");  
  44.             s.Append("Content-Disposition:  form-data;  name=\"" + name + "\";  filename=\"" + file.Name + "\"\r\n");  
  45.             s.Append("Content-Type: " + GetContentType(file) + "\r\n");  
  46.             s.Append("\r\n");  
  47.             AppendString(s.ToString());  
  48.             AppendBytes(GetFileBytes(file));  
  49.             AppendString("\r\n");       
  50.         }  
  51.         byte[] GetFileBytes(FileInfo f)  
  52.         {  
  53.             FileStream fs = new FileStream(f.FullName, FileMode.Open);  
  54.             BinaryReader br = new BinaryReader(fs);  
  55.             byte[] b = br.ReadBytes((int)f.Length);  
  56.             br.Close();  
  57.             fs.Close();  
  58.             return b;  
  59.         }  
  60.         string GetContentType(FileInfo f)  
  61.         {  
  62.           // return "application/octet-stream";  //取消注释此行,则不再区分是否为图片  
  63.             System.Drawing.Image image = null;  
  64.             FileStream fs = new FileStream(f.FullName, FileMode.Open);  
  65.             try  
  66.             {  
  67.                 image = System.Drawing.Image.FromStream(fs);  
  68.             }  
  69.             catch (ArgumentException)  
  70.             {  
  71.                 // 文件无法被解析为一个图片  
  72.                 return "application/octet-stream";  
  73.             }  
  74.             finally   
  75.             {  
  76.                 fs.Close();  
  77.             }  
  78.             return  GetFormat(image.RawFormat);  
  79.         }  
  80.         string GetFormat(ImageFormat imf)   
  81.         {  
  82.             if (ImageFormat.Bmp.Equals(imf))  
  83.             {  
  84.                 return "image/bmp";  
  85.             }  
  86.             if (ImageFormat.Jpeg.Equals(imf))   
  87.             {  
  88.                 return "image/jpeg";  
  89.             }  
  90.             if (ImageFormat.Png.Equals(imf))   
  91.             {  
  92.                 return "image/png";  
  93.             }  
  94.             return "application/octet-stream";  
  95.         }  
  96.         void AppendBytes(byte[] bytes)   
  97.         {  
  98.             byte[] newByte = new byte[bs.Length + bytes.Length];  
  99.             Array.Copy(bs, 0, newByte, 0, bs.Length);  
  100.             Array.Copy(bytes, 0, newByte, bs.Length, bytes.Length);  
  101.             bs = newByte;  
  102.         }  
  103.         void AppendString(string value)  
  104.         {  
  105.             byte[] bytes = Encoding.UTF8.GetBytes(value);  
  106.             AppendBytes(bytes);  
  107.         }  
  108.         void EndData()  
  109.         {  
  110.             StringBuilder s = new StringBuilder();  
  111.             s.Append("--").Append(boundary).Append("--\r\n");  
  112.             s.Append("\r\n");  
  113.             AppendString(s.ToString());  
  114.         }  
  115.     }  
  116. }  

在我本机测试,中文没有出现乱码。

c#调用方法:

 

C#代码  
  1. string url = "http://localhost:3000/up_load";  
  2. string imgPath = @"D:\素材\圆月.jpg";  
  3. HttpPostUtil hp = new HttpPostUtil(url);  
  4. hp.AddTextParameter("text","中文");  
  5. hp.AddFileParameter("img",new FileInfo(imgPath));  
  6. byte[] b = hp.Send();  
  7.  Console.WriteLine(Encoding.UTF8.GetString(b));  

 

如有错误,请补充下。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
通过Android 客户端上传数据到服务器
Android上传文件到服务器
一.HTTP协议:
Android端通过HttpURLConnection上传文件到服务器
android上传手机图片至服务器
Windows Phone 7下一个上传图片的类_银光中国 Silverlight 资源 ...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服