導航:首頁 > 版本升級 > httpclient接收文件

httpclient接收文件

發布時間:2023-09-03 06:22:43

1. http接收xml數據用java怎麼實現呀

  1. 用HttpClient接收得到你的xml文件,將文件保存本地

  2. 解析xml文件,Dom,Sax都行,也可藉助第三方版XStrame解決,

  3. 解析對比,權將得到的xml與本地xml比較,找出不同,生成文檔

  4. 發送給你想發送的人

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);
}
閱讀全文

與httpclient接收文件相關的資料

熱點內容
vba打開讀取excel文件 瀏覽:915
編程如何結束程序 瀏覽:82
免root腳本精靈代碼 瀏覽:196
百世的如來神掌app如何寄件 瀏覽:755
root文件怎麼刪除 瀏覽:495
為什麼說編程語言不是和計算機交 瀏覽:674
美食用什麼app推薦 瀏覽:621
javalearn 瀏覽:422
大數據大專畢業論題有哪些 瀏覽:117
學數控編程技校去哪裡 瀏覽:759
如何在編程中編輯時鍾 瀏覽:908
手機怎麼打開隱藏文件夾 瀏覽:147
js原生attr 瀏覽:65
蘋果6p屏幕密碼忘記可以刷機嗎 瀏覽:569
升級系統文件小說 瀏覽:713
新建圖形文件的方法有哪些 瀏覽:676
網路營銷的現實方式有哪些 瀏覽:887
excel如何從其他表格提取所需數據 瀏覽:196
怎麼固定安卓wifimac地址 瀏覽:382
遼寧5g網路覆蓋地區有哪些 瀏覽:820

友情鏈接