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

舍利云:海外云服务器,6核16G超大带宽vps;支持全球范围,原价516,折后价200元/月!

舍利云怎么样?舍利云推出了6核16G超大带宽316G高性能SSD和CPU,支持全球范围,原价516,折后价200元一月。原价80美元,现价30美元,支持地区:日本,新加坡,荷兰,法国,英国,澳大利亚,加拿大,韩国,美国纽约,美国硅谷,美国洛杉矶,美国亚特兰大,美国迈阿密州,美国西雅图,美国芝加哥,美国达拉斯。舍利云是vps云服务器的销售商家,其产品主要的特色是适合seo和建站,性价比方面非常不错,...

HostSailor:罗马尼亚机房,内容宽松;罗马尼亚VPS七折优惠,罗马尼亚服务器95折

hostsailor怎么样?hostsailor成立多年,是一家罗马尼亚主机商家,机房就设在罗马尼亚,具说商家对内容管理的还是比较宽松的,商家提供虚拟主机、VPS及独立服务器,今天收到商家推送的八月优惠,针对所有的产品都有相应的优惠,商家的VPS产品分为KVM和OpenVZ两种架构,OVZ的比较便宜,有这方面需要的朋友可以看看。点击进入:hostsailor商家官方网站HostSailor优惠活动...

hosthatch:14个数据中心15美元/年

hosthatch在做美国独立日促销,可能你会说这操作是不是晚了一个月?对,为了准备资源等,他们拖延到现在才有空,这次是针对自己全球14个数据中心的VPS。提前示警:各个数据中心的网络没有一个是针对中国直连的,都会绕道而且ping值比较高,想买的考虑清楚再说!官方网站:https://hosthatch.com所有VPS都基于KVM虚拟,支持PayPal在内的多种付款方式!芝加哥(大硬盘)VPS5...

httpclient4为你推荐
桌面背景图片非主流想下载非主流桌面背景,有没有专业的背景平台提供下载啊?视频制作软件哪个好什么视频编辑软件比较适合小白的免费阅读小说app哪个好什么小说软件好用又免费华为p40和mate30哪个好华为p40手机。跟荣耀30哪个好?游戏盒子哪个好游戏盒子哪个好?核芯显卡与独立显卡哪个好核心显卡和独立显卡哪个好oppo和vivo哪个好Vivo和OPPO哪个好点啊?红茶和绿茶哪个好红茶和绿茶哪个好?51空间登录以前的51空间怎么进?首选dns服务器地址默认网关和首选DNS服务器是多少
空间域名 黑龙江域名注册 双线主机租用 edgecast 外国空间 域名评估 服务器托管什么意思 绍兴电信 卡巴斯基免费试用版 优酷黄金会员账号共享 阿里云邮箱登陆 买空间网 ubuntu安装教程 ftp是什么东西 ddos攻击 usb大容量存储设备 大容量存储控制器驱动 web服务器安全配置 宿迁服务器托管 彩虹云点播破解版 更多