当个文件上传就可以了,给你一个下例子,你可以试试:public int sendUrlData(String url,String path) { File targetFile = new File(path); MultipartPostMethod filePost = new MultipartPostMethod(url); HttpClient client = null; long startdate = new Date().getTime(); System.out.println("开始时间:"+startdate); int status = 0; try { client = new HttpClient(); FilePart part = new FilePart("file",targetFile); part.setCharSet( "utf-8 "); part.setContentType( "application/octet-stream "); filePost.addPart(part);
要是HTTP将文件传到web服务器某目录下的的话,要模拟http请求,httpclient应该可以做到。
也或者使用socket或者nio可以通讯传递文件。
File应该如何得到?本地上的可以很好的到,但是服务器的应该怎么得到
File targetFile = new File(path);
MultipartPostMethod filePost = new MultipartPostMethod(url);
HttpClient client = null;
long startdate = new Date().getTime();
System.out.println("开始时间:"+startdate);
int status = 0;
try {
client = new HttpClient();
FilePart part = new FilePart("file",targetFile);
part.setCharSet( "utf-8 ");
part.setContentType( "application/octet-stream ");
filePost.addPart(part);
HttpClientParams httparams=new HttpClientParams();
httparams.setSoTimeout(60000);
client.setParams(httparams);
status = client.executeMethod(filePost);
filePost.releaseConnection();
} catch (FileNotFoundException e) {
status = 0;
} catch (HttpException e) {
status = 0;
} catch (IOException e) {
status = 0;
}
long endDate = new Date().getTime();
System.out.println("时间差:"+(endDate-startdate)/1000+"========="+status);
return status;
} 注意导入相关的httpClient包!这样,就可以通过http请求带一个文件为参数传送到web服务器,服务器在通过开发去处理了。