基于HTTP协议的文件传输

txf_7337 2009-06-26 02:51:15
有两台电脑,我想把一台电脑上的文件拷到另一台上,当中的传输协议要基于HTTP的,谁能给点提示或者示例
...全文
726 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
云上飞翔 2009-06-26
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 txf_7337 的回复:]
非常谢谢,你这个是下载,我想要的是上传的。
需求是这样的:客户端选择本地文件,然后点击上传。当然中间是通过HTTP上传
[/Quote]
答:原来是这样。楼主先用Swing做好界面,先择好文件,然后单击“上传”,调用如下的代码,完成多文件的上传功能[设服务器方,负责处理上传的JSP文件是:http://yourhost:8080/cgi/process_file_upload.jsp] :

private static String url =
"http://yourhost:8080/cgi/process_file_upload.jsp";
HttpClient client = new HttpClient();
client.setConnectionTimeout(8000);


//用POST方式上传文件
File f = new File("/path/myfileToUpload.txt");
PostMethod filePost = new PostMethod(url);
Part[] parts = {
new StringPart("param_name", "value"),
new FilePart(f.getName(), f)
};
filePost.setRequestEntity(
new MultipartRequestEntity(parts, filePost.getParams())
);
int statusCode1 = client.executeMethod(filePost);


txf_7337 2009-06-26
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 ddyouyue 的回复:]
jspsmartupload
[/Quote]
我现在是在swing里实现。。。
如果能得到struts中的form,不。。。。获得request我就满足了
ddyouyue 2009-06-26
  • 打赏
  • 举报
回复
jspsmartupload
txf_7337 2009-06-26
  • 打赏
  • 举报
回复
非常谢谢,你这个是下载,我想要的是上传的。
需求是这样的:客户端选择本地文件,然后点击上传。当然中间是通过HTTP上传
云上飞翔 2009-06-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 txf_7337 的回复:]
引用 7 楼 jiangnaisong 的回复:
使用HttpClient就行了。


我用到HttpServer了。。。不过写的自己都不知道是什么了。。。
请问你有示例吗?
[/Quote]
答:有啊。以下代码是用HttpClient去Get一个你要下载的文件:http://www.mysite.com/download.tar.gz

public class MyHttpClientGetFile {
public static String url =
"http://www.mysite.com/download.tar.gz";

public static void main(String[] args){

//创建HttpClient
HttpClient httpClient = new HttpClient();

//创建 method instance
GetMethod getMethod = new GetMethod(url);

try{

//执行Get方法
int statusCode =
httpClient.executeMethod(getMethod);

//读取响应。
InputStream in =
getMethod.getResponseBodyAsStream();

byte[] b = new byte[1024];//1K缓冲区读
int len;

while ((len = in.read(b)) != -1) {
//将数据写到。。。。 System.out
//你可改为:存到文件中。
for(int i = 0; i < len; i++){
System.out.print((char)b[i]);
}
}

in.close();

}catch(HttpException e1){
//System.out.println(e1);
}catch(IOException e2){
//System.out.println(e2);
}finally{
//释放连接
getMethod.releaseConnection();
}

}

}

txf_7337 2009-06-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jiangnaisong 的回复:]
使用HttpClient就行了。
[/Quote]
我用到HttpServer了。。。不过写的自己都不知道是什么了。。。
请问你有示例吗?
txf_7337 2009-06-26
  • 打赏
  • 举报
回复
楼上的,你是通过TCP传输的。。。。
henry_fuzr 2009-06-26
  • 打赏
  • 举报
回复
httpServer + httpClient 下面是个简单的示例

在这个基础上改改。


package com.fzr.learn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoService {

private int port = 8999;

private ServerSocket serverSocket;

public EchoService() throws IOException {

serverSocket = new ServerSocket(port);// 创建tcp服务对象
System.out.println("服务启动");

}

public String echo(String message) {
return "echo : " + message;
}

private PrintWriter getWriter(Socket socket) throws IOException {
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut, true);
}

private BufferedReader getReader(Socket socket) throws IOException {
InputStream socketIn = socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}

public void service() {
while (true) {
Socket socket = null;
System.out.println("11");
try {
socket = serverSocket.accept();
System.out.println("new connection accepted "
+ socket.getInetAddress() + ":" + socket.getPort());
System.out.println(socket);
BufferedReader br = getReader(socket);
PrintWriter pw = getWriter(socket);
String message = null;
System.out.println("22");
while((message=br.readLine())!=null){
System.out.println("33");
System.out.println(message);
pw.println(echo(message));
if(message.equals("bye")){
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(socket!=null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

}
}

public static void main(String[] args) throws IOException {
new EchoService().service();
System.out.println("aa");
}

}




package com.fzr.learn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class EchoClient {

private String host = "localhost";
private int port = 8999;

private Socket socket ;

public EchoClient () throws UnknownHostException, IOException{
socket = new Socket (host,port);

}

private PrintWriter getWriter(Socket socket) throws IOException {
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut, true);
}

private BufferedReader getReader(Socket socket) throws IOException {
InputStream socketIn = socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}

public void talk(){
try {
BufferedReader br = getReader(socket);
PrintWriter pw = getWriter(socket);
BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in));
String message =null ;
while((message=localReader.readLine())!=null){
pw.println(message);
System.out.println(br.readLine());
if(message.equals("bye")){
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

public static void main(String[] args) throws UnknownHostException, IOException {
new EchoClient().talk();
}



}



云上飞翔 2009-06-26
  • 打赏
  • 举报
回复
[Quote=引用楼主 txf_7337 的帖子:]
有两台电脑,我想把一台电脑上的文件拷到另一台上,当中的传输协议要基于HTTP的,谁能给点提示或者示例
[/Quote]
答:使用HttpClient就行了。
zuoguodang 2009-06-26
  • 打赏
  • 举报
回复
可以基于TCP协议写个程序
zuijiejina 2009-06-26
  • 打赏
  • 举报
回复
google 搜索http java实现
哇,一大堆呢。。
txf_7337 2009-06-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zuijiejina 的回复:]
建议楼主找个java的实现,然后在基础上进行你需要的修改。
[/Quote]
找不到啊,推荐个吧
zuijiejina 2009-06-26
  • 打赏
  • 举报
回复
建议楼主找个java的实现,然后在基础上进行你需要的修改。
txf_7337 2009-06-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zuijiejina 的回复:]
找http协议看看,然后自己写个。。
[/Quote]
正在进行。。。
不过有点提示可以少走很多弯路啊
zuijiejina 2009-06-26
  • 打赏
  • 举报
回复
找http协议看看,然后自己写个。。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧