Chinaunix首页 | 论坛 | 博客
  • 博客访问: 71151
  • 博文数量: 33
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 220
  • 用 户 组: 普通用户
  • 注册时间: 2014-07-15 22:22
文章分类

全部博文(33)

文章存档

2016年(3)

2015年(23)

2014年(7)

我的朋友

分类: Java

2015-12-16 11:04:35

帮助文档

the user enters "Larry" in the text input, and selects the text file "file1.txt", the user agent might send back the following data:

 Content-Type: multipart/form-data; boundary=AaB03x

   --AaB03x
   Content-Disposition: form-data; name="submit-name"

   Larry
   --AaB03x
   Content-Disposition: form-data; name="files"; filename="file1.txt"
   Content-Type: text/plain

   ... contents of file1.txt ...
   --AaB03x--

注意:网上编写的代码可能编译环境的不同,代码的表示也是不同的,再或者本身没有实现,一定找到规定书写的帮助文档,直接对照书写代码。
具体的代码实现:
String urlStr = " ";
URL url = new URL(urlStr);
HttpURLConnection http = (HttpURLConnection) url.openConnection(); 
http.setRequestMethod("POST");
        //上传文件形式 "multipart/form-data"
http.setRequestProperty("Content-Type", "multipart/form-data; boundary=----footfoodapplicationrequestnetwork");
      //boundary自定义的分隔符的类型
 
http.setRequestProperty(
                    "Accept",
                    "image/gif,   image/x-xbitmap,   image/jpg,   image/pjpeg,   application/vnd.ms-excel,   application/vnd.ms-powerpoint,                                  application/msword,   application/x-shockwave-flash,   application/x-quickviewplus,   */*");
   http.setDoOutput(true);        
   http.setDoInput(true);
 //若提交为post方式,需要修改为false
   http.setUseCaches(false);
   System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
   System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒
   http.connect();
   
   fin = new FileInputStream(filepath);
   byte[] bytes = new byte[1024];
   
   BufferedOutStream os = null;
 //输入数据
   os = new BufferedOutputStream(http.getOutputStream());     
               //注意格式  --boundary
               //name="files"  (文件类型)filename="pic_1.jpg" (有很多地方是说是根路径,其实不是就是单纯的文件名)
   buffer.append("------footfoodapplicationrequestnetwork\r\n");  
   buffer.append("Content-Disposition:form-data;name=\"files\";" );
   buffer.append("filename=\""); 
   buffer.append("pic_1.jpg"+"\"");
   buffer.append("\r\n");  
   buffer.append("Content-Type: image/jpg");    //如果是图片,或者音频,文本文件类型的那么还需要隔一行进行添加内容
   buffer.append("\r\n\r\n");
   os.write(buffer.toString().getBytes());

   int bytelength;
while((bytelength=fin.read(bytes))!=-1){
   os.write(bytes, 0, bytelength);
   }
//文件传输结束格式:--boundary--
       os.write("\r\n------footfoodapplicationrequestnetwork--\r\n".getBytes()); 
   
//接收网络返回内容
   ins = http.getInputStream();
   int size = ins.available();
   byte[] jsonBytes = new byte[size];
   ins.read(jsonBytes);
   String message = new String(jsonBytes, "utf-8");
   JSONObject demoJson = new JSONObject(message);
   String media_id = demoJson.getString("media_id");
   if(media_id!=null){
    System.out.println(media_id);
       return "图片上传成功!";
   }

                                   
阅读(3251) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~