java http协议发送_java 采用socket模拟HTTP协议发送文件到服务器

/**

采用JAVA的socket模拟HTTP请求发送文件,希望对大家有帮助!!!

@auther zhaojie

@since 2014-6-22 02:44:12

*/

package com.fileupload.bean;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.net.Socket;

import java.net.UnknownHostException;

public class TestClient {

public static void main(String[] args) throws UnknownHostException, IOException {

uploadFile("127.0.0.1", 8080, "E:/dd.pdf", "/HttpProtot/servlet/aa.servlet", "fileName", "sss.bin", "7d00013333");

}

/**

*

*

* */

public static void uploadFile(String host,Integer port,String fileAbsoPath,String servletName,String fileparaName,String fileName,String boundary) {

FileInputStream fin = null;

OutputStream out = null;

InputStream in = null;

try{

Socket socket = new Socket(host,port);

socket.setTcpNoDelay(true);

socket.setKeepAlive(true);

socket.setSoTimeout(100000000);

File f = new File(fileAbsoPath);//需要上传的文件

fin = new FileInputStream(f);

StringBuffer fileBefore = new StringBuffer();

///fileBefore:文件上传的头信息

fileBefore.append("--").append(boundary).append("\r\n");

fileBefore.append("Content-Disposition:form-data; name=\""+fileparaName+"\"; filename=\""+fileName+"\"").append("\r\n");

fileBefore.append("Content-Type: application/octet-stream").append("\r\n");

fileBefore.append("\r\n");

//文件上传完之后的尾部信息

String end = new String("\r\n--"+boundary+"--");

//参数长度

long length=f.length()+fileBefore.toString().getBytes().length+end.getBytes().length;

out = socket.getOutputStream();

StringBuffer header = new StringBuffer();

//headre:http请求头信息

//

header.append("POST "+servletName+" HTTP/1.1").append("\r\n");

header.append("Host:"+host+":"+port+"").append("\r\n");

header.append("Accept:text/html, application/xhtml+xml, */*").append("\r\n");

header.append("Accept-Language:zh-CN").append("\r\n");

header.append("Content-Type:multipart/form-data; boundary="+boundary).append("\r\n");

header.append("Content-Length:").append(length).append("\r\n");

header.append("Connection:Keep-Alive").append("\r\n");

header.append("\r\n");

//报文结构:

//第一部分:组装http头部信息

out.write(header.toString().getBytes());

//第二部分:组装文件头部信息

out.write(fileBefore.toString().getBytes());

//第三部分:组装文件信息

byte b[]=new byte[1024];

int rb = 0;

while((rb = fin.read(b))!=-1){

out.write(b,0,rb);

};

fin.close();

//组装文件尾部信息

out.write(end.getBytes());

out.flush();

//服务器返回信息

in = socket.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(in));

String line =null;

while((line=br.readLine())!=null){

System.out.println(line);

}

br.close();

}

catch (Exception e) {

e.printStackTrace();

}

finally {

try {

if (fin != null) {

fin.close();

}

} catch (Exception ee) {

ee.printStackTrace();

}

try {

if (out != null) {

out.close();

}

} catch (Exception ee) {

ee.printStackTrace();

}

try {

if (in != null) {

in.close();

}

} catch (Exception ee) {

ee.printStackTrace();

}

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值