按步骤来,不然会出错。。。。。。
1.下载两个文件commons-io和commons-fileupload(直接在Goole搜索这两个关键字,然后直接在apache官方网站下载就行了),然后解压导入他们的jar文件到你的项目中
2.随便写一个上传文件的JSP页面,如下:
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%@ page import="java.util.*" %>
- <html>
- <head>
- <title>文件上传测试</title>
- </head>
- <body>
- <form method="post" action="<%=request.getContextPath() %>/servlet/FileUpload" enctype="multipart/form-data" >
- <p><input type="file" name="myfile1" value="浏览文件" /></p>
- <p><input type="submit" value="上传"/></p>
- </form>
- </body>
- </html>
3.文件上传页面好了以后,就是见项目中建一个servlet文件,我这里建的servlet是FileUpload,在web.xml的配置文件是
- <servlet>
- <servlet-name>FileUpload</servlet-name>
- <servlet-class>com.cneric.blog.FileUpload</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>FileUpload</servlet-name>
- <url-pattern>/servlet/FileUpload</url-pattern>
- </servlet-mapping>
servlet的代码如下:
- package com.cneric.blog;
- import java.io.File;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- 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.DiskFileUpload;
- import org.apache.commons.fileupload.FileItem;
- import org.apache.commons.fileupload.FileItemFactory;
- import org.apache.commons.fileupload.disk.DiskFileItemFactory;
- import org.apache.commons.fileupload.servlet.ServletFileUpload;
- public class FileUpload extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("UTF-8");
- // 构建上传路径
- String basePath = getServletContext().getRealPath("/Upload");
- System.out.println("path=" + basePath);
- // 文件上傳部分
- boolean isMultipart = ServletFileUpload.isMultipartContent(request);
- if (isMultipart == true) {
- try {
- FileItemFactory factory = new DiskFileItemFactory();
- ServletFileUpload upload = new ServletFileUpload(factory);
- // 得到所有的表单域,它们目前都被当作FileItem
- List<FileItem> fileItems = upload.parseRequest(request);
- Iterator<FileItem> iter = fileItems.iterator();
- // 依次处理每个表单域
- while (iter.hasNext()) {
- FileItem item = (FileItem) iter.next();
- if (item.isFormField()) {
- // 如果item是正常的表单域
- String name = item.getFieldName();
- String value = item.getString();
- System.out.print("表单域名为:" + name + "表单域值为:" + value);
- } else {
- // 如果item是文件上传表单域
- // 获得文件名及路径
- String fileName = item.getName();
- System.out.println(fileName);
- if (fileName != null) {
- File fullFile = new File(item.getName());
- // 如果文件存在则上传
- if (fullFile.exists()) {
- File fileOnServer = new File(basePath, fullFile
- .getName());
- item.write(fileOnServer);
- System.out.println("文件"
- + fileOnServer.getName() + "上传成功");
- }
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- System.out.println("the enctype must be multipart/form-data");
- }
- // 取得服务器中已有文件的下載列表
- List<String> fileListInServer = new ArrayList<String>();
- File dir = new File(basePath);
- String[] children = dir.list();
- if (children != null) {
- for (int i = 0; i < children.length; i++) {
- fileListInServer.add(children[i]);
- }
- }
- request.setAttribute("downloadList", fileListInServer);
- // 跳回原頁面
- RequestDispatcher dispatcher = request
- .getRequestDispatcher("/fileupload/test.jsp");
- dispatcher.forward(request, response);
- return;
- }
- }
这里要注意上传路径的问题,在本例子中,我是将文件上传到项目的一个目录中,在WebRoot目录下
建立一个Upload目录,用于存放上传的文件,则在代码中构建上传的路径为
String basePath = getServletContext().getRealPath("/Upload");