教授通过代码示例学习Commons HTTPClient4组件的编程技术——实现文件上传和下载

httpclient4  时间:2021-01-29  阅读:()

杨教授工作室精心创作的优秀程序员职业提升必读系列资料

目录

1.1 实现文件上传和下载. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2

1.1.1 完善客户端文件下载相关的程序功能. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2

1.1.2 通过HT TP实现文件上传功能——M ult ip artEnt ity类. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5

1.1.3 应用Mult ip art Ent ity实现文件上传. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6

杨教授工作室版权所有盗版必究 1/16页

杨教授工作室精心创作的优秀程序员职业提升必读系列资料

1. 1 实现文件上传和下载

1. 1. 1完善客户端文件下载相关的程序功能

1、在客户端项目中添加一个DownLoadIma ge类

1类名称为DownLoadImage包名称为co m.p x1987.httpc lient

2编程DownLoadImage程序代码package com.px1987.httpclient;import java. io.FileOutputStream;import java. io. IOException;import java. io. InputStream;import org.apache.http.HttpEntity;

杨教授工作室精心创作的优秀程序员职业提升必读系列资料import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http. impl.client.DefaultHttpClient;public class DownLoadImage {public DownLoadImage() throws ClientProtocolException, IOException {

//创建一个客户端类似打开一个浏览器

HttpClient httpClient = new DefaultHttpClient() ;

String targetFileURL="http://127.0.0. 1 :8080/webbank/images/logo. jpg";

HttpGet oneGetMethod = new HttpGet(targetFileURL) ;

HttpResponse httpResponse =httpClient. execute(oneGetMethod) ;int httpStatusCode=httpResponse.getStatusLine() .getStatusCode() ;if(httpStatusCode==HttpStatus.SC_OK) {

System.out.println(httpResponse.getStatusLine() ) ;//打印服务器返回的状态

HttpEntity entity = httpResponse.getEntity() ;if (entity != null) {

//这里可以得到文件的类型如image/jpg /zip /tiff等

System.out.println(entity.getContentType() ) ;

//可以判断是否是文件数据流

System.out.println(entity. isStreaming() ) ;downLoadSomeOneFile(entity) ;

}entity.consumeContent() ; //确保资源释放

}else{

System.out.println("方法执行过程中出现了错误") ;//打印服务器返回的状态

杨教授工作室版权所有盗版必究 3/16页

杨教授工作室精心创作的优秀程序员职业提升必读系列资料

}oneGetMethod.abort() ; //结束本次请求httpClient.getConnectionManager() . shutdown() ; ;//释放连接

}public void downLoadSomeOneFile(HttpEntity entity ) throws IOException{

InputStream oneInputStream = entity.getContent() ;

String outputTargetFileName="logo. jpg";

FileOutputStream oneFileOutputStream = newFileOutputStream(outputTargetFileName) ;byte[] buffer= new byte[4096] ;int length = -1 ;while ( (length = oneInputStream.read(buffer) ) != -1) {oneFileOutputStream.write(buffer, 0, length) ;

}oneFileOutputStream.close() ;oneInputStream.close() ;

}public static void main(String[] args) throws ClientProtocolException,IOException {

DownLoadImage oneWebBankAppclient=new DownLoadImage() ;

}

}

2、执行后的结果

首先启动服务器、并正确地部署Web应用系统后并保证在Web系统的目录中存在有一个图片文件

杨教授工作室版权所有盗版必究 4/16页

杨教授工作室精心创作的优秀程序员职业提升必读系列资料

再执行DownLoadImage后的结果

1. 1.2通过HTTP实现文件上传功能——MultipartEntity类

1、Mult ip art E nt ity类的主要功能

HttpC lie nt4组件使用了单独的一个Multip artEntity类包装处理上传的文件Mult ip art E nt ity表示由多个独立的数据类型实体组成的数据。

杨教授工作室精心创作的优秀程序员职业提升必读系列资料

2、在Web服务器端项目中添加与文件上传功能实现有关的系统库

1 Commons-F ileUp load组件的系统库文件commons-fileup load-1.2.1.jar

2实现IO功能的commons-io.j ar 它其实是Commons IO组件的一个系统库文件

它是对Java IO库的功能扩展组件简化和扩展Java IO编程然后再将这两个系统库文件添加到项目的classpath环境变量中对于Web应用系统而言 同样也还是放在WEB-INF/lib目录中。

1. 1.3应用MultipartEntity实现文件上传

1、在项目中添加一个处理文件上传的Servlet

1类名称为UpLo adF ileServlet包名称为com.px1987.httpclient.servlet

杨教授工作室精心创作的优秀程序员职业提升必读系列资料

2 URL-Pattern设置为/upLoadFileServlet

3编程该Servletpackage com.px1987.httpclient. servlet;import java. io.File;import java. io. IOException;

杨教授工作室精心创作的优秀程序员职业提升必读系列资料import java. io.PrintWriter;import java.util.HashMap;import java.util. Iterator;import java.util.List;import java.util.Map;import javax. servlet.RequestDispatcher;import javax. servlet.ServletException;import javax. servlet.http.HttpServlet;import javax. servlet.http.HttpServletRequest;import javax. servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.FileUploadException;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload. servlet.ServletFileUpload;public class UpLoadFileServlet extends HttpServlet {private static final long serialVersionUID = 1L;public UpLoadFileServlet() {super() ;

}public void destroy() {super.destroy() ;

}private static final String upLoadFileSavedDirectoryInServerDisk="/upload/";String upLoadFilePathInServerDisk =null;public void doPost (HttpServletRequest request, HttpServletResponseresponse) throws ServletException, IOException {doUpLoadFile(request,response) ;

}public void doUpLoadFile(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException{

杨教授工作室版权所有盗版必究 8/16页

杨教授工作室精心创作的优秀程序员职业提升必读系列资料upLoadFilePathInServerDisk =this.getServletContext() .getRealPath(upLoadFileSavedDirectoryInServerDisk) ;

List<FileItem> upLoadFileFormItems=null;try {upLoadFileFormItems=getAllItemInUpLoadForm(request) ;

} catch (FileUploadException e1) {e1.printStackTrace() ;

}

Map<String,FileItem> allItemsInRegisterFormHashMap=getAllItemInUpLoadForm(request,upLoadFileFormItems) ;

String headImageFileDescript=

( (FileItem)allItemsInRegisterFormHashMap.get("headImageFileDescript") ) .getString() ;

FileItem headImageFile=

(FileItem)allItemsInRegisterFormHashMap.get("headImageFile") ;

String upLoadFileNameAndPath = headImageFile.getName() ;if (upLoadFileNameAndPath == null | |upLoadFileNameAndPath. trim() . length() == 0) {request. setAttribute("errorText", "请选择需要上传的文件然后再继续进行上传操作 ") ;forwardTargetPage("/index. jsp",request,response) ;return;

}

String upLoadFileName=upLoadFileNameAndPath. substring(upLoadFileNameAndPath. lastIndexOf(File. separator) + 1) ;

String upLoadFileExtendName =

杨教授工作室版权所有盗版必究 9/16页

OneTechCloud香港/日本/美国CN2 GIA月付9折季付8折,可选原生IP或高防VPS

OneTechCloud(易科云)是一家主打CN2等高端线路的VPS主机商家,成立于2019年,提供的产品包括VPS主机和独立服务器租用等,数据中心可选美国洛杉矶、中国香港、日本等,有CN2 GIA线路、AS9929、高防、原生IP等。目前商家针对全场VPS主机提供月付9折,季付8折优惠码,优惠后香港VPS最低季付64元起(≈21.3元/月),美国洛杉矶CN2 GIA线路+20Gbps防御型VPS...

onevps:新增(支付宝+中文网站),香港/新加坡/日本等9机房,1Gbps带宽,不限流量,仅需$4/月

onevps最新消息,为了更好服务中国区用户:1、网站支付方式新增了支付宝,即将增加微信;原信用卡、PayPal方式不变;(2)可以切换简体中文版网站,在网站顶部右上角找到那个米字旗,下拉可以换中国简体版本。VPS可选机房有:中国(香港)、新加坡、日本(东京)、美国(纽约、洛杉矶)、英国(伦敦)、荷兰(阿姆斯特丹)、瑞士(苏黎世)、德国(法兰克福)、澳大利亚(悉尼)。不管你的客户在亚太区域、美洲区...

CloudServer:$4/月KVM-2GB/50GB/5TB/三个数据中心

CloudServer是一家新的VPS主机商,成立了差不多9个月吧,提供基于KVM架构的VPS主机,支持Linux或者Windows操作系统,数据中心在美国纽约、洛杉矶和芝加哥机房,都是ColoCrossing的机器。目前商家在LEB提供了几款特价套餐,最低月付4美元(或者$23.88/年),购买更高级别套餐还能三个月费用使用6个月,等于前半年五折了。下面列出几款特别套餐配置信息。CPU:1cor...

httpclient4为你推荐
滚筒洗衣机和波轮洗衣机哪个好波轮洗衣机和滚桶洗衣机哪个好?哪个更实用?燃气热水器和电热水器哪个好燃气热水器与电热水器的优缺点?免费阅读小说app哪个好哪个手机小说app比较好用呢?华为p40和mate30哪个好荣耀30pro和华为P40哪个好?手动挡和自动挡哪个好自动挡和手动挡哪个更好一点浮动利率和固定利率哪个好房贷须知:固定还是浮动利率好车险哪个好买汽车保险,买哪几种比较好红茶和绿茶哪个好红茶和绿茶哪个更好?网络机顶盒哪个好什么牌子的网络机顶盒好用?视频软件哪个好编辑视频用什么软件最好
韩国虚拟主机 便宜域名注册 vps优惠码cnyvps enzu yardvps 电影服务器 java主机 wordpress技巧 512m unsplash realvnc 网页背景图片 好看qq空间 52测评网 帽子云 cloudlink 空间租赁 空间登陆首页 网站加速软件 免费的asp空间 更多