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);
}