杨教授工作室精心创作的优秀程序员职业提升必读系列资料
目录
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页
IMIDC发布了6.18大促销活动,针对香港、台湾、日本和莫斯科独立服务器提供特别优惠价格最低月付30美元起。IMIDC名为彩虹数据(Rainbow Cloud),是一家香港本土运营商,全线产品自营,自有IP网络资源等,提供的产品包括VPS主机、独立服务器、站群独立服务器等,数据中心区域包括香港、日本、台湾、美国和南非等地机房,CN2网络直连到中国大陆。香港服务器 $39/...
vollcloud怎么样?vollcloud LLC创立于2020年,是一家以互联网基础业务服务为主的 技术型企业,运营全球数据中心业务。VoLLcloud LLC针对新老用户推出全场年付产品7折促销优惠,共30个,机会难得,所有产品支持3日内无条件退款,同时提供产品免费体验。目前所有产品中,“镇店之宝”产品性价比高,适用大部分用户基础应用,卖的也是最好,同时,在这里感谢新老用户的支持和信任,我们...
在前面的文章中就有介绍到半月湾Half Moon Bay Cloud服务商有提供洛杉矶DC5数据中心云服务器,这个堪比我们可能熟悉的某服务商,如果我们有用过的话会发现这个服务商的价格比较贵,而且一直缺货。这里,于是半月湾服务商看到机会来了,于是有新增同机房的CN2 GIA优化线路。在之前的文章中介绍到Half Moon Bay Cloud DC5机房且进行过测评。这次的变化是从原来基础的年付49....