杨教授工作室精心创作的优秀程序员职业提升必读系列资料
目录
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页
EtherNetservers是一家成立于2013年的英国主机商,提供基于OpenVZ和KVM架构的VPS,数据中心包括美国洛杉矶、新泽西和杰克逊维尔,商家支持使用PayPal、支付宝等付款方式,提供 60 天退款保证,这在IDC行业来说很少见,也可见商家对自家产品很有信心。有需要便宜VPS、多IP VPS的朋友可以关注一下。优惠码SUMMER-VPS-15 (终身 15% 的折扣)SUMMER-...
关于HostYun主机商在之前也有几次分享,这个前身是我们可能熟悉的小众的HostShare商家,主要就是提供廉价主机,那时候官方还声称选择这个品牌的机器不要用于正式生产项目,如今这个品牌重新转变成Hostyun。目前提供的VPS主机包括KVM和XEN架构,数据中心可选日本、韩国、香港和美国的多个地区机房,电信双程CN2 GIA线路,香港和日本机房,均为国内直连线路,访问质量不错。今天和大家分享下...
hostsailor怎么样?hostsailor成立多年,是一家罗马尼亚主机商家,机房就设在罗马尼亚,具说商家对内容管理的还是比较宽松的,商家提供虚拟主机、VPS及独立服务器,今天收到商家推送的八月优惠,针对所有的产品都有相应的优惠,商家的VPS产品分为KVM和OpenVZ两种架构,OVZ的比较便宜,有这方面需要的朋友可以看看。点击进入:hostsailor商家官方网站HostSailor优惠活动...