当前位置:首页
开发技术指南» 文章正文
    引言:
 

 

    摘要: 我要做一个在线教育的网站 ,信息量比较多 ,有用户登录 ,分类 ,推荐 ,下载排行 ,等 等 ,我正愁着怎么分配这些东西 ,才既显得简洁 ,又能把所有的内容全显示出来 , 大家有什么好主意 ,介绍一下。 ......
 ·session的延迟大惑不解    »显示摘要«
    摘要: 日前学asp.net时,遇到个session的问题。写了个东西验证,代码如下: private void page_load(object sender, system.eventargs e) { // 在此处放置用户代码以初始化页面 if(!ispostback) session["test"] = false; else { ......


怎么用java模仿客户端来实现http的put请求上传一个文件到服务器上

请问怎么用java模仿客户端来实现http的put请求上传一个文件到服务器上

NO.1   作者: cbhyk

http://expert.csdn.net/Expert/topic/2238/2238658.xml?temp=1.171291E-03  
   
  import   java.io.File;  
  import   java.io.FileInputStream;  
  import   java.io.IOException;  
  import   java.io.OutputStream;  
  import   java.net.InetAddress;  
  import   java.net.Socket;  
   
  public   class   SimulateUpload  
  {  
          private   static   final   byte[]   BOUNDARY   =   "---------------------------7d33bc1b47038c".getBytes();  
          private   static   final   byte[]   CRLF   =   "\r\n".getBytes();  
   
          public   void   simUpload(String   page,   String[]   paramNames,   Object[]   paramValues,   OutputStream   os)   throws   IOException  
          {  
                  int   contentLength   =   calculateLength(paramNames,   paramValues);  
   
                  os.write(("POST   "   +   page   +   "   HTTP/1.1").getBytes());  
                  os.write(CRLF);  
   
                  os.write("User-Agent:   myselfHttp/1.0".getBytes());  
                  os.write(CRLF);  
   
                  os.write("Accept:   */*".getBytes());  
                  os.write(CRLF);  
   
                  os.write("Host:   ".getBytes());  
                  os.write(InetAddress.getLocalHost().getHostAddress().getBytes());  
                  os.write(CRLF);  
   
                  os.write("Content-Type:   multipart/form-data;   boundary=".getBytes());  
                  os.write(BOUNDARY);  
                  os.write(CRLF);  
   
                  os.write(("Content-Length:   "   +   contentLength).getBytes());  
                  os.write(CRLF);  
   
                  os.write(CRLF);  
   
                  os.write(BOUNDARY);  
                  os.write(CRLF);  
                  for(int   i=0;   i<paramNames.length;   i++)  
                  {  
                          if(paramValues[i]   instanceof   String)  
                                  writeParameter(os,   paramNames[i],   (String)   paramValues[i]);  
                          else   if(paramValues[i]   instanceof   File)  
                                  writeFile(os,   paramNames[i],   (File)   paramValues[i]);  
                  }  
                  os.flush();  
          }  
   
          private   void   writeFile(OutputStream   os,   String   paramName,   File   file)   throws   IOException  
          {  
                  os.write("Content-Disposition:   form-data;   name=\"".getBytes());  
                  os.write(paramName.getBytes());  
                  os.write("\";   filename=\"".getBytes());  
                  os.write(file.getAbsolutePath().getBytes());  
                  os.write("\"\r\nContent-Type:   application/octet-stream".getBytes());  
                  os.write(CRLF);  
                  os.write(CRLF);  
   
                  FileInputStream   fis   =   new   FileInputStream(file);  
                  byte[]   buf   =   new   byte[4096];  
                  int   bytes   =   0;  
                  while((bytes   =   fis.read(buf))   !=   -1)  
                          os.write(buf,   0,   bytes);  
                  fis.close();  
   
                  os.write(CRLF);  
                  os.write(BOUNDARY);  
                  os.write(CRLF);  
          }  
   
          private   void   writeParameter(OutputStream   os,   String   name,   String   value)   throws   IOException  
          {  
                  os.write("Content-Disposition:   form-data;   name=\"".getBytes());  
                  os.write(name.getBytes());  
                  os.write(");  
                  os.write(CRLF);  
                  os.write(CRLF);  
                  os.write(value.getBytes());  
                  os.write(CRLF);  
                  os.write(BOUNDARY);  
                  os.write(CRLF);  
          }  
   
          private   int   calculateLength(String[]   paramNames,   Object[]   paramValues)  
          {  
                  int   result   =   BOUNDARY.length   +   CRLF.length;  
                  for(int   i=0;   i<paramNames.length;   i++)  
                  {  
                          if(paramValues[i]   instanceof   String)  
                                  result   +=   88   +   paramNames[i].getBytes().length   +   ((String)paramValues[i]).getBytes().length;  
                          else   if(paramValues[i]   instanceof   File)  
                          {  
                                  File   file   =   (File)   paramValues[i];  
                                  result   +=   141   +   paramNames[i].getBytes().length   +   file.getAbsolutePath().getBytes().length   +   file.length();  
                          }  
                  }  
                  return   result;  
          }  
   
          public   static   void   main(String[]   args)  
          {  
                  try  
                  {  
                          Socket   socket   =   new   Socket("localhost",   80);  
                          OutputStream   os   =   socket.getOutputStream();  
                          SimulateUpload   sim   =   new   SimulateUpload();  
                          String[]   paramNames   =   new   String[]{"param1",   "param2",   "param3",   "file",   "file2"};  
                          Object[]   paramValues   =   new   Object[]{"value1",   "value2",   "value3",   new   File("d:\\test.txt"),   new   File("d:\\test1.txt")};  
                          sim.simUpload("/uploadServlet",   paramNames,   paramValues,   os);  
                          os.flush();  
                          os.close();  
                          socket.close();  
                  }  
                  catch(Exception   e)  
                  {  
                          e.printStackTrace();  
                  }  
          }  
  }  
   
 


 ·网段连接问题(送100分)    »显示摘要«
    摘要: 我有三台机,主机a在网段192.168.2.0/24上ip为192.168.2.4(mask:255.255.255 0),主机b在网段192.168.4.0/24上,ip地址为192.168.4.111(mask:255.255.255.0),主机c准备用作路由器,有两张网卡,ip地址分别为192.168.2.29(mask:255.255.255.0)和192.168.4.29(mas......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE