java中使用scoket模拟http post请求发送图片或文件


最近遇到个问题,两个系统共用用户的头像,用户的头像在一个系统中保存,这就涉及到将图片通过scoket发送给另一个系统存储的问题,最初的思路是将图片读成byte[]数组,然后发送,但又发现,发送图片的同时还要发送密钥、uid、username等信息,好通过对方系统的验证,这就不好弄了,最后想出来,将图片读成字符串、然后模拟http post请求,将图片和密钥、uid等信息发送过去,在服务器端,将得到的byte[]数组写进文件中,最后实验真的成功了,我做了整理和消化,也有不足,请指教,代码如下:

 

Java代码   收藏代码
  1. //客户端  
  2. public static String readFileAsString(String fileName) throws Exception {  
  3.             FileInputStream fis = new FileInputStream(fileName);  
  4.             BufferedInputStream in = new BufferedInputStream(fis);  
  5.             byte buffer[] = new byte[256];  
  6.             StringBuffer picStr=new StringBuffer();  
  7.             BASE64Encoder base64=new BASE64Encoder();  
  8.             while (in.read(buffer)>= 0){  
  9.                 picStr.append(base64.encode(buffer));//进行64位编码  
  10.             }  
  11.             fis.close();  
  12.             fis=null;  
  13.             in.close();  
  14.             in=null;  
  15.             buffer=null;  
  16.             return picStr.toString();  
  17.         }  
  18.        
  19.         public static void main(String[] a) {     
  20.             try {  
  21.                 File file=new File("c:/雪狼突击队.jpg");  
  22.                 //将文件读成字符串  
  23.                 String picString=readFileAsString(file.toString());  
  24.                 //URLEncode  
  25.                 picString="picdata="+URLEncoder.encode(picString, "UTF-8");  
  26.                 String url="http://localhost:8080/Test/index.jsp?uid=1&username=test&auth=098f6bcd4621d373cade4e832627b4f6";  
  27.                 Socket socket =new Socket(InetAddress.getByName(url),80);  
  28.                 DataOutputStream dos=new DataOutputStream(socket.getOutputStream());     
  29.                 String message=""  
  30.                 +"POST   "+url+" HTTP/1.1 \r\n "  
  31.                 +"Host: test.lingye.com \r\n "+"Accept: */* \r\n "  
  32.                 +"Cache-Control:no-cache \r\n" +"User-Agent: MSIE6.0; \r\n "  
  33.                 +"Content-Type: application/x-www-form-urlencoded   \r\n "   
  34.                 +"Content-Length: "+picString.length()+" \r\n "  
  35.                 +"Connection: Close   \r\n\r\n"//报头以两个\n作为结束标志  
  36.                 +picString+"\r\n ";//post数据    
  37.                 byte buffer[]=message.getBytes();     
  38.                 dos.write(buffer);  
  39.                 dos.flush();  
  40.                 dos.close();  
  41.                 //以上只进行了发送操作  
  42.                 socket.close();  
  43.             } catch (Exception e) {  
  44.                 e.printStackTrace();  
  45.             }   
  46.         }     
  47.   
  48. //服务器端  
  49. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  50.         String picdata=request.getParameter("picdata");  
  51.         BASE64Decoder base64=new BASE64Decoder();  
  52.         //64位解码  
  53.         byte[] buffer=base64.decodeBuffer(picdata);  
  54.         //写进文件  
  55.         FileOutputStream fos=new FileOutputStream("c:/雪狼突击队1.jpg");  
  56.         fos.write(buffer);  
  57.         fos.flush();  
  58.         fos.close();  
  59.         fos=null;  
  60.     } 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值