您的位置:首页 > 理论基础 > 计算机网络

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

2009-09-04 22:37 966 查看
//客户端
public static String readFileAsString(String fileName) throws Exception {

FileInputStream fis = new FileInputStream(fileName);
BufferedInputStream in = new BufferedInputStream(fis);
byte buffer[] = new byte[256];

StringBuffer picStr=new StringBuffer();
BASE64Encoder base64=new BASE64Encoder();
while (in.read(buffer)>= 0){
picStr.append(base64.encode(buffer));//进行64位编码

}
fis.close();
fis=null;
in.close();
in=null;
buffer=null;
return picStr.toString();
}

public static void main(String[] a) {

try {
File file=new File("c:/雪狼突击队.jpg");
//将文件读成字符串

String picString=readFileAsString(file.toString());
//URLEncode

picString="picdata="+URLEncoder.encode(picString, "UTF-8");
String url="http://localhost:8080/Test/index.jsp?uid=1&username=test&auth=098f6bcd4621d373cade4e832627b4f6";
Socket socket =new Socket(InetAddress.getByName(url),80);
DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
String message=""
+"POST "+url+" HTTP/1.1 /r/n "
+"Host: test.lingye.com /r/n "+"Accept: */* /r/n "
+"Cache-Control:no-cache /r/n" +"User-Agent: MSIE6.0; /r/n "
+"Content-Type: application/x-www-form-urlencoded /r/n "
+"Content-Length: "+picString.length()+" /r/n "
+"Connection: Close /r/n/r/n"//报头以两个/n作为结束标志

+picString+"/r/n ";//post数据

byte buffer[]=message.getBytes();
dos.write(buffer);
dos.flush();
dos.close();
//以上只进行了发送操作

socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}

//服务器端
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String picdata=request.getParameter("picdata");
BASE64Decoder base64=new BASE64Decoder();
//64位解码

byte[] buffer=base64.decodeBuffer(picdata);
//写进文件

FileOutputStream fos=new FileOutputStream("c:/雪狼突击队1.jpg");
fos.write(buffer);
fos.flush();
fos.close();
fos=null;
}

//客户端
public static String readFileAsString(String fileName) throws Exception {
FileInputStream fis = new FileInputStream(fileName);
BufferedInputStream in = new BufferedInputStream(fis);
byte buffer[] = new byte[256];
StringBuffer picStr=new StringBuffer();
BASE64Encoder base64=new BASE64Encoder();
while (in.read(buffer)>= 0){
picStr.append(base64.encode(buffer));//进行64位编码
}
fis.close();
fis=null;
in.close();
in=null;
buffer=null;
return picStr.toString();
}

public static void main(String[] a) {
try {
File file=new File("c:/雪狼突击队.jpg");
//将文件读成字符串
String picString=readFileAsString(file.toString());
//URLEncode
picString="picdata="+URLEncoder.encode(picString, "UTF-8");
String url="http://localhost:8080/Test/index.jsp?uid=1&username=test&auth=098f6bcd4621d373cade4e832627b4f6";
Socket socket =new Socket(InetAddress.getByName(url),80);
DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
String message=""
+"POST   "+url+" HTTP/1.1 /r/n "
+"Host: test.lingye.com /r/n "+"Accept: */* /r/n "
+"Cache-Control:no-cache /r/n" +"User-Agent: MSIE6.0; /r/n "
+"Content-Type: application/x-www-form-urlencoded   /r/n "
+"Content-Length: "+picString.length()+" /r/n "
+"Connection: Close   /r/n/r/n"//报头以两个/n作为结束标志
+picString+"/r/n ";//post数据
byte buffer[]=message.getBytes();
dos.write(buffer);
dos.flush();
dos.close();
//以上只进行了发送操作
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}

//服务器端
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String picdata=request.getParameter("picdata");
BASE64Decoder base64=new BASE64Decoder();
//64位解码
byte[] buffer=base64.decodeBuffer(picdata);
//写进文件
FileOutputStream fos=new FileOutputStream("c:/雪狼突击队1.jpg");
fos.write(buffer);
fos.flush();
fos.close();
fos=null;
}


代码很粗糙,大家看原理就好了,我试过上传.rar也是可以的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: