⽂件上传之Apachecommonsfileupload使⽤ ⽂件上传的⽅法主要⽬前有两个常⽤的,⼀个是SmartUpload,⼀个是Apache的Commons fileupload.
我们这⾥主要介绍下第⼆个的⽤法,⾸先要上传⽂件,注意⼏个问题:
1 form表单内,要添加空间<input type="file" name="myfile">
2 form表单的内容格式要定义成multipart/form-data格式
3 需要类库:1 commons-io.jar 2commons-fileupload-1.3.1.jar
接下来我们看下他的⽤法。
⾸先阅读Apache commons fileupload的官⽅⽂档可以发现下⾯⼏个常⽤的函数:
1 创建⽂件解析对象
DiskFileUpload diskFileUpload = new DiskFileUpload();
2 进⾏⽂件解析后放在List中,因为这个类库⽀持多个⽂件上传,因此把结果会存在List中。
List<FileItem> list = diskFileUpload.parseRequest(request);
3 获取上传⽂件,进⾏分析(不是必须)
File remoteFile = new File(new Name().getBytes(),"UTF-8"));
4 创建新对象,进⾏流拷贝
file1 = new ServletContext().getRealPath("attachment"),Name());
InputStream ins = InputStream();
OutputStream ous = new FileOutputStream(file1);
try{
byte[] buffer = new byte[1024];
int len = 0;
while((len = ad(buffer)) > -1)
ous.write(buffer,0,len);
out.println("以保存⽂件"+AbsolutePath()+"<br/>");
}finally{
ous.close();
教师的英语怎么说ins.close();
}
这样我们就完成了⽂件的上传。
fileUpload.html
<form action="servlet/UploadServlet" method="post" enctype="multipart/form-data">
<div align="center">
<fieldset >
<legend>上传⽂件</legend><br/>
<div align="left">上传⽂件1</div>
<div align="left">
<input type="file" name="file1"/>
</div>
<div align="left">上传⽂件2</div>
<div align="left">
<input type="file" name="file2"/>
</div>
<div>
<div align='left'>上传⽂件说明1</div>
<div align='left'><input type="text" name="description1"/></div>
</div>
<div>
<div align='left'>上传⽂件说明2</div>
<div align='left'><input type="text" name="description2"/></div>
</div>
<div>
<div align='left'>
<input type='submit' value="上传⽂件"/>
</div>
</div>
</fieldset>
</div>
</form>
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class&st.hello.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/servlet/UploadServlet</url-pattern>
</servlet-mapping>
UploadServlet.java
st.hello;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apachemons.fileupload.DiskFileUpload;
import org.apachemons.fileupload.FileItem;
import org.apachemons.fileupload.FileUploadException;
public class UploadServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public UploadServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/
**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post. *
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
@SuppressWarnings({ "unchecked", "deprecation" })
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
File file1 = null,file2=null;
String description1 = null,description2 = null;
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = Writer();
DiskFileUpload diskFileUpload = new DiskFileUpload();
try{
List<FileItem> list = diskFileUpload.parseRequest(request);
out.println("遍历所有的<br/>");
for(FileItem fileItem : list){
if(fileItem.isFormField()){
if("description1".FieldName())){
out.println("遍历到description1 ... <br/>");
description1 = new String().getBytes(),"UTF-8");
}
if("description2".FieldName())){
out.println("遍历到description2 ... <br/>");
description2 = new String().getBytes(),"UTF-8");
}
}else{
if("file1".FieldName())){
File remoteFile = new File(new Name().getBytes(),"UTF-8"));
out.println("遍历到<br/>");
out.println("客户端⽂件位置:"+AbsolutePath()+"<br/>");
file1 = new ServletContext().getRealPath("attachment"),Name()); ParentFile().mkdirs();
InputStream ins = InputStream();
OutputStream ous = new FileOutputStream(file1);
try{
byte[] buffer = new byte[1024];
int len = 0;
while((len = ad(buffer)) > -1)
ous.write(buffer,0,len);
out.println("以保存⽂件"+AbsolutePath()+"<br/>");
}finally{
ous.close();
ins.close();
}
}
if("file2".FieldName())){
如何瘦腰腹
File remoteFile = new File(new Name().getBytes(),"UTF-8"));
out.println("遍历到<br/>");
out.println("客户端⽂件位置:"+AbsolutePath()+"<br/>");
file2 = new ServletContext().getRealPath("attachment"),Name()); ParentFile().mkdirs();
InputStream ins = InputStream();
OutputStream ous = new FileOutputStream(file2);
try{
byte[] buffer = new byte[1024];
int len = 0;
while((len = ad(buffer)) > -1)
ous.write(buffer,0,len);
out.println("以保存⽂件"+AbsolutePath()+"<br/>");
}finally{
ous.close();
ins.close();
}
}
}
out.println("Request 解析完毕<br/><br/>");
}
}catch(FileUploadException e){}
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
我的播音系女友if(file1 != null){
out.println("<div>");
out.println(" <div align='left'>file1;</div>");
out.println(" <div align='left'><a href='"+ContextPath()+"/attachment/"+
out.println("</div>");
out.println("</div>");
}
if(file2 != null){
out.println("<div>");
out.println(" <div align='left'>file2;</div>");
out.println(" <div align='left'><a href='"+ContextPath()+"/attachment/"+ Name()+"'target=_blank>"+Name()+"</a>");
out.println("</div>");
out.println("</div>");
}
out.println("<div>");
out.println(" <div align='left'>description1:</div>");
out.println(" <div align='left'>");
out.println(description1);
out.println("</div>");
out.println("</div>");
out.println("<div>");
out.println(" <div align='left'>description2:</div>");
out.println(" <div align='left'>");
out.println(description2);
out.println("</div>");
out.println("</div>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
微波炉使用// Put your code here
}
}
View Code
新驾照考试科目运⾏⽰例
发布评论