1. http接收xml数据用java怎么实现呀
用HttpClient接收得到你的xml文件,将文件保存本地
解析xml文件,Dom,Sax都行,也可借助第三方版XStrame解决,
解析对比,权将得到的xml与本地xml比较,找出不同,生成文档
发送给你想发送的人
2. 通过 java (servlet) 实现两个web工程间的文件传输
这个思路很简单,如下:
1、访问A的servlet,我们在这个Servlet里面取到这个文件,这个很容易是吧
2、在A的servlet将取到的文件(inputstrema格式),以post的形式,模拟表单提交给B的servlet
3、在B的servlet里面接收,就像接收普通的表单上传的一样
下面是一些上传和接收的核心代码,使用的httpclient
A里面的上传:
HttpClient httpclient = new DefaultHttpClient();
String url = “这里是B的servlet的访问的地址,全地址”;
HttpPost httppost = new HttpPost(url);
// 一个本地的文件
InputStreamBody fileis = new InputStreamBody(is, fileName);
// 多部分的实体
MultipartEntity reqEntity = new MultipartEntity();
// 增加
reqEntity.addPart("bin", fileis);
// 设置
httppost.setEntity(reqEntity);
HttpResponse responses = null;
try {
responses = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (Validator.isNotNull(responses)) {
int httpstatus = responses.getStatusLine().getStatusCode();
if(httpstatus!=200){
System.out.println(url+"访问错误,http状态"+httpstatus);
}
}
B里面接收文件的核心代码,使用的fileupload
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = new ArrayList();
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
FileItem file = null;
if (items.size()>0) {
file =(FileItem)items.get(0);
}
3. java 读取服务器上的文件
File file = new File(ServletActionContext.getServletContext().getRealPath(“/”),"test.txt");
"/"是web项目的根目录,然后就和读本地文件的一样
4. HttpClient获取文件流的一部分,该怎么解决
在请求头里设置Range,可以拿到不同的部分,前提还需要webserver支持。
/**
*开始下载
*@throwsException
*/
publicvoidstartDown()throwsException{
HttpClienthttpClient=newDefaultHttpClient();
try{
//获取下载文件信息
getDownloadFileInfo(httpClient);
//启动多个下载线程
startDownloadThread();
//开始监视下载数据
monitor();
}catch(Exceptione){
throwe;
}finally{
httpClient.getConnectionManager().shutdown();
}
}
/**
*获取下载文件信息
*/
(HttpClienthttpClient)throwsIOException,
ClientProtocolException,Exception{
HttpHeadhttpHead=newHttpHead(url);
HttpResponseresponse=httpClient.execute(httpHead);
//获取HTTP状态码
intstatusCode=response.getStatusLine().getStatusCode();
if(statusCode!=200)thrownewException("资源不存在!");
if(getDebug()){
for(Headerheader:response.getAllHeaders()){
System.out.println(header.getName()+":"+header.getValue());
}
}
//Content-Length
Header[]headers=response.getHeaders("Content-Length");
if(headers.length>0)
contentLength=Long.valueOf(headers[0].getValue());
httpHead.abort();
httpHead=newHttpHead(url);
httpHead.addHeader("Range","bytes=0-"+(contentLength-1));
response=httpClient.execute(httpHead);
if(response.getStatusLine().getStatusCode()==206){
acceptRanges=true;
}
httpHead.abort();
}
/**
*启动多个下载线程
*@throwsIOException
*@throwsFileNotFoundException
*/
()throwsIOException,
FileNotFoundException{
//创建下载文件
Filefile=newFile(localPath);
file.createNewFile();
RandomAccessFileraf=newRandomAccessFile(file,"rw");
raf.setLength(contentLength);
raf.close();
//定义下载线程事件实现类
=newDownloadThreadListener(){
publicvoidafterPerDown(DownloadThreadEventevent){
//下载完一个片段后追加已下载字节数
synchronized(object){
DownloadTask.this.receivedCount+=event.getCount();
}
}
publicvoiddownCompleted(DownloadThreadEventevent){
//下载线程执行完毕后从主任务中移除
threads.remove(event.getTarget());
if(getDebug()){
System.out.println("剩余线程数:"+threads.size());
}
}
};
//不支持多线程下载时
if(!acceptRanges){
if(getDebug()){
System.out.println("该地址不支持多线程下载");
}
//定义普通下载
DownloadThreadthread=newDownloadThread(url,0,contentLength,file,false);
thread.addDownloadListener(listener);
thread.start();
threads.add(thread);
return;
}
//每个请求的大小
longperThreadLength=contentLength/threadCount+1;
longstartPosition=0;
longendPosition=perThreadLength;
//循环创建多个下载线程
do{
if(endPosition>=contentLength)
endPosition=contentLength-1;
DownloadThreadthread=newDownloadThread(url,startPosition,endPosition,file);
thread.addDownloadListener(listener);
thread.start();
threads.add(thread);
startPosition=endPosition+1;//此处加1,从结束位置的下一个地方开始请求
endPosition+=perThreadLength;
}while(startPosition<contentLength);
}