当前位置:编程学习 > JAVA >>

求教求大神教教利用httpclient实现文件上传问题

public void uploadFile(File file, String url) {  
         if (!file.exists()) {  
            return;  
         }  
         PostMethod postMethod = new PostMethod(url);  
         try {  
          Part[] parts = { new FilePart(file.getName(), file) };
          postMethod.setRequestEntity(new MultipartRequestEntity(parts,postMethod.getParams()));
              HttpClient client = new HttpClient();
              client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
              int status = client.executeMethod(postMethod);
                  if (status == HttpStatus.SC_OK)
                  {
                   System.out.println("上传成功"+postMethod.getResponseBodyAsString());
                   // 上传成功
                  }
                  else
                  {
                   System.out.println("上传失败"+status);
                   // 上传失败
                  }

         } catch (Exception e) {  
             e.printStackTrace();  
        } finally {  
            //释放连接  
            postMethod.releaseConnection();  
         }  
     }  
  
    /** 
      * @param args 
     */  
     public static void main(String[] args) {  
         Test test = new Test();  
         test.uploadFile(new File("D://toBOS/f.txt"),  
             "http://127.0.0.1:8080/test/");  
     }




这个运行怎么不是报404就是报302?求高手 --------------------编程问答-------------------- 有人没 --------------------编程问答-------------------- 求指教啊。 --------------------编程问答-------------------- --------------------编程问答-------------------- 都吃饭去了。。 我在, 但是我不会。。 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答--------------------
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,