疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

javaweb项目apache组件实现文件上传


 

    javaweb项目经常需要文件的上传下载,今天介绍一种简单的apache 的组件来实现文件上传comm
 
    首先需要两个jar包的支持commons-fileupload-1.3.jar commons-io-2.4.jar放入WEB_INF的lib包下
 
    上传的界面代码:
 
    upload.html
 
    <html>
 
    <head>
 
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
 
    <title>File upload</title>
 
    </head>
 
    <body>
 
    <form name="myform" action="upload.jsp" method="post"
 
    enctype="multipart/form-data">
 
    Text1:<br>
 
    <input type="text" name="name"><br>
 
    File2:<br>
 
    <input type="file" name="myfile"><br>
 
    <br>
 
    <input type="submit" name="submit" value="Commit">
 
    </form>
 
    </body>
 
    </html>
    上传的处理jsp代码:
 
    <%@ page language="java" contentType="text/html; charset=GBK"
 
    pageEncoding="GB18030"%>
 
    <%@ page import="org.apache.commons.fileupload.*"%>
 
    <%@ page import="org.apache.commons.fileupload.servlet.*"%>
 
    <%@ page import="org.apache.commons.fileupload.disk.*"%>
 
    <%@ page import="java.util.*"%>
 
    <%@ page import="java.io.*"%>
 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
    <%
 
    String uploadPath="f:\fileupload";
 
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
 
    if(isMultipart==true){
 
    try{
 
    FileItemFactory factory = new DiskFileItemFactory();
 
    ServletFileUpload upload = new ServletFileUpload(factory);
 
    List<FileItem> items = upload.parseRequest(request);//得到所有的文件
 
    Iterator<FileItem> itr = items.iterator();
 
    while(itr.hasNext()){//依次处理每个文件
 
    FileItem item=(FileItem)itr.next();
 
    String fileName=item.getName();//获得文件名,包括路径
 
    if(item.isFormField()){
 
    System.out.println("name ============== " + item.getFieldName()+new String(item.getString()。getBytes("iso8859-1"),"GBK"));
 
    }
 
    else if(fileName!=null){
 
  File savedFile=new File(uploadPath,item.getName());
 
    item.write(savedFile);
 
    }
 
    }
 
    out.print("upload succeed");
 
    }
 
    catch(Exception e){
 
    e.printStackTrace();
 
    }
 
    }
 
    else{
 
    out.println("the enctype must be multipart/form-data");
 
    }
 
    %>
 
    <html>
 
    <head>
 
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
 
    <title>File upload</title>
 
    </head>
 
    <body>
 
    </body>
 
    </html>
 
    upload2.html
 
    <html>
 
    <head>
 
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
 
    <title>File upload</title>
 
    </head>
 
    <body>
 
    <form name="myform" action="servlet/UploadServlet" method="post"
 
    enctype="multipart/form-data">
 
    Text1:<br>
 
    <input type="text" name="name"><br>
 
    File2:<br>
 
    <input type="file" name="myfile"><br>
 
    <br>
 
    <input type="submit" name="submit" value="Commit">
 
    </form>
 
    </body>
 
    </html>
 
    下面使用servle来处理上传:
 
    UploadServlet.java
 
    package com.zj.sample;
 
    import java.io.File;
 
    import java.io.IOException;
 
    import java.util.Iterator;
 
    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.apache.commons.fileupload.FileItem;
 
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
 
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
 
    @SuppressWarnings("serial")
 
    public class UploadServlet extends HttpServlet {
 
    private String uploadPath = "f:\fileupload"; // 上传文件的目录
 
    private String tempPath = "f:\fileupload\buffer\"; // 临时文件目录
 
    File tempPathFile;
 
    @SuppressWarnings("unchecked")
 
    public void doPost(HttpServletRequest request, HttpServletResponse response)
 
    throws IOException, ServletException {
 
    try {
 
    DiskFileItemFactory factory = new DiskFileItemFactory();
 
    factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
 
    factory.setRepository(tempPathFile);// 设置缓冲区目录
 
    ServletFileUpload upload = new ServletFileUpload(factory);
 
    upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
 
    List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
 
    Iterator<FileItem> i = items.iterator();
 
    while (i.hasNext()) {
 
    FileItem fi = (FileItem) i.next();
 
    if (fi.isFormField()) {
 
    System.out.println("name ============== " + fi.getFieldName()+new String(fi.getString()。getBytes("iso8859-1"),"GBK"));
 
    } else {
 
    String fileName = fi.getName();
 
    if (fileName != null) {
 
    File savedFile = new File(uploadPath,fileName);
 
    fi.write(savedFile);
 
    }
 
    }
 
    }
 
    System.out.print("upload succeed");
 
    } catch (Exception e) {
 
    // 可以跳转出错页面
 
    e.printStackTrace();
 
    }
 
    }
 
    public void init() throws ServletException {
 
    File uploadFile = new File(uploadPath);
 
    if (!uploadFile.exists()) {
 
    uploadFile.mkdirs();
 
    }
 
    File tempPathFile = new File(tempPath);
 
    if (!tempPathFile.exists()) {
 
    tempPathFile.mkdirs();
 
    }
 
    }
 
    }