教授通过代码示例学习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页

搬瓦工:香港PCCW机房即将关闭;可免费升级至香港CN2 GIA;2核2G/1Gbps大带宽高端线路,89美元/年

搬瓦工怎么样?这几天收到搬瓦工发来的邮件,告知香港pccw机房(HKHK_1)即将关闭,这也不算是什么出乎意料的事情,反而他不关闭我倒觉得奇怪。因为目前搬瓦工香港cn2 GIA 机房和香港pccw机房价格、配置都一样,可以互相迁移,但是不管是速度还是延迟还是丢包率,搬瓦工香港PCCW机房都比不上香港cn2 gia 机房,所以不知道香港 PCCW 机房存在还有什么意义?关闭也是理所当然的事情。点击进...

ZJI全新上架香港站群服务器,4C段238个IP月付1400元起

ZJI本月新上线了香港葵湾机房站群服务器,提供4个C段238个IPv4,支持使用8折优惠码,优惠后最低每月1400元起。ZJI是原Wordpress圈知名主机商家:维翔主机,成立于2011年,2018年9月更名为ZJI,提供中国香港、台湾、日本、美国独立服务器(自营/数据中心直营)租用及VDS、虚拟主机空间、域名注册等业务,所选数据中心均为国内普遍访问速度不错的机房。葵湾二型(4C站群)CPU:I...

Hostodo(年付$34.99), 8TB月流量 3个机房可选

Hostodo 算是比较小众的海外主机商,这次九月份开学季有提供促销活动。不过如果我们有熟悉的朋友应该知道,这个服务商家也是比较时间久的,而且商家推进活动比较稳,每个月都有部分活动。目前有提供机房可选斯波坎、拉斯维加斯和迈阿密。从机房的地理位置和实际的速度,中文业务速度应该不是优化直连的,但是有需要海外业务的话一般有人选择。以前一直也持有他们家的年付12美元的机器,后来用不到就取消未续约。第一、开...

httpclient4为你推荐
软银赛富民信排在投资公司第几qq空间首页现在QQ空间首页能做吗免费阅读小说app哪个好有什么免费读小说的软件?清理手机垃圾软件哪个好清理手机垃圾的软件哪个好空间登录器用什么登录器可以登录QQ(除了QQ登录器)qq空间登录电脑手机怎么登qq空间电脑版?考生个人空间登录第一次登陆湖南省高等教育自学考试 考生个人空间就密码不对为什么360云盘下载别人在百度知道给了你360云盘资源,怎么在360云盘使用????360云盘企业版360云盘转企业版我的数据该怎么办360云盘共享群360网盘怎样进共享群?
长沙服务器租用 域名备案网站 美国主机代购 双11抢红包攻略 http500内部服务器错误 华为4核 godaddy域名证书 权嘉云 dux 数字域名 河南m值兑换 昆明蜗牛家 电信主机 免费邮件服务器 网通服务器 秒杀品 xuni 乐视会员免费领取 789电视剧网 windowsserverr2 更多