java发送HTTP GET POST请求

1、发送源码

import  java.io.BufferedReader;
import  java.io.BufferedWriter;
import  java.io.File;
import  java.io.FileReader;
import  java.io.IOException;
import  java.io.OutputStream;
import  java.io.OutputStreamWriter;
import  java.net.HttpURLConnection;
import  java.net.URL;



/**
 * 通过HTTP上传文件
 * 
@author  cnyqaio@hotmail.com
 * @create 2008-4-14
 
*/

public   class  PostUrl {

    HttpURLConnection conn 
=   null ;
    BufferedWriter bWriter 
=   null ;
    OutputStream os 
=   null ;
    OutputStreamWriter osw 
=   null ;
    FileReader fr 
=   null ;
    BufferedReader br 
=   null ;
    String str 
=   "" ;
    
char  c  =   ' /n ' ;
    String filename 
=   " d:/shchinamobile.log " ;

    

    
/**
     * 通过HTTP协议以POST形式发送指定文件至指定url
     * 
@param  url
     * 
@throws  IOException
     
*/
    
public   void  sendPost(String url) {
        
        
try  {
            File f 
=   new  File(filename);
            
            URL url1 
=   new  URL(url);
            conn 
=  (HttpURLConnection)url1.openConnection();
            conn.setRequestMethod(
" POST " );
            conn.setRequestProperty(
" Content-Type " " application/x-www-form-urlencoded " );
            conn.setRequestProperty(
" Connection " " Keep-Alive " );
            conn.setUseCaches( 
false );
            conn.setDoOutput(
true );
            osw 
=   new  OutputStreamWriter(conn.getOutputStream());
            fr 
=   new  FileReader(f);
            br 
=   new  BufferedReader(fr);
            
while ((str  =  br.readLine())  !=   null ) {
                osw.write(str);
                osw.write(c);
            }
            osw.flush() ;
            osw.close() ;
            
            
if  (conn.getResponseCode()  !=  HttpURLConnection.HTTP_OK)
                System.out.println( 
" connect failed! " );
        } 
catch  (IOException e) {
            e.printStackTrace(System.out);
        }
        
finally
        {
            
if  (osw  !=   null )
                
try  {
                    osw.close() ;
                } 
catch  (IOException e1) {
                    e1.printStackTrace(System.out);
                }
            
            
if  (conn  !=   null )
                conn.disconnect() ;
        }
    }    

    
public   static   void  main(String[] args) {
        PostUrl pu 
=   new  PostUrl();
        
try  {
            pu.sendPost(
" http://127.0.0.1:8080/testjava/testp.jsp " );
        } 
catch  (Exception e) {
            e.printStackTrace();
        }
    }
}


2、接收JSP

<%
     System.err.println(
" This a struts page. " );
     
         
// 接收上传的文件内容的临时文件的文件名
        String tempFileName = new  String( " tempFileName1 " );
        
// tempFile1对象指向这个临时文件
        File tempFile1 = new  File( " D:/ " ,tempFileName);
        
// outputFile1文件输出流指向这个临时文件
        FileOutputStream outputFile1 = new  FileOutputStream(tempFile1);
        
// 得到客户端提交的所有数据
        InputStream fileSource1 = request.getInputStream();
        
// 将得到的客户端的数据写入临时文件
         byte  b[] = new   byte [ 1000 ];
        
int  n;
        
while ((n = fileSource1.read(b)) !=- 1 )
            outputFile1.write(b,
0 ,n);
        
// 关闭输出流和输入流
        outputFile1.close();
        fileSource1.close();
      
     
%>


已测试通过

还可以通HttpClient来发送,请参照:http://www.blogjava.net/willpower88/archive/2008/04/14/192679.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值