62,614
社区成员
发帖
与我相关
我的任务
分享
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);
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();
}
}
}
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();
}
}