首页 > 代码库 > Java 文件上传

Java 文件上传

一、使用commons-fileupload实现文件上传

  1、下载组件:

  commons-fileupload组件:http://commons.apache.org/proper/commons-fileupload/download_fileupload.cgi

  commons-io组件:http://commons.apache.org/proper/commons-io/download_io.cgi

 

  2、将jar包导入到项目中。

  技术分享

  3、上传页面

<!-- enctype="multipart/form-data":将表单以二进制格式进行提交-->    <form action="upload.jsp" method="post" enctype="multipart/form-data">        <input type="file" name="file1" />        <input type="submit" value="上传" />        <%            if(request.getAttribute("imgurl") != null){                String imgurl = request.getAttribute("imgurl").toString();                out.println("<img src=http://www.mamicode.com/‘" + imgurl + "‘ />");            }            if(request.getAttribute("msg") != null){                out.print(request.getAttribute("msg"));            }        %>    </from>

 

  4、表单处理页面

<%@page import="java.io.File"%><%@page import="java.text.SimpleDateFormat"%><%@page import="org.apache.commons.fileupload.FileItem"%><%@page import="java.util.*"%><%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%><%@page import="org.apache.commons.fileupload.FileItemFactory"%><%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%><%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%    //判断表单是否以multipart方式提交    if (ServletFileUpload.isMultipartContent(request)) {        //创建FileItemFactory实例        FileItemFactory factory = new DiskFileItemFactory();        //创建ServletFileUpload实例        ServletFileUpload upload = new ServletFileUpload(factory);        //获取页面中所有提交过来的信息        List<FileItem> items = upload.parseRequest(request);        //设置上传文件格式        List<String> exts = new ArrayList<String>();        exts.add(".jpg");        exts.add(".gif");        exts.add(".png");                for (FileItem item : items) {            //判断元素类型,true为普通表单元素,false为文件表单元素            if (!item.isFormField()) {                if (item.getSize() < 1024 * 1024) {                    //获取文件名称                    String filename = item.getName();                    String fileext = filename.substring(filename.lastIndexOf("."));                    if (exts.contains(fileext)) {                        //设置文件上传的绝对路径                        String uploadpath = this.getServletContext().getRealPath("uploads");                        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssms");                        String newfilename = sdf.format(new Date()) + fileext;                        File file = new File(uploadpath + "/" + newfilename);                        item.write(file);                        request.setAttribute("imgurl", "uploads/" + newfilename);                    } else {                        request.setAttribute("msg", "只能上传.jpg、.gif、.png格式的图片");                    }                }else{                    request.setAttribute("msg", "最大只能上传1M的文件!");                }            }            request.getRequestDispatcher("index.jsp").forward(request, response);        }    }%>

 

  上传并显示图片,注意:图片是上传到 tomcat发布文件夹中,而不是在eclipse文件夹中。

 

Java 文件上传