JSP 上传文件


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
      <servlet-name>FileUpload</servlet-name>
      <servlet-class>servlets.FileUpload</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>FileUpload</servlet-name>
      <url-pattern>/FileUpload</url-pattern>
  </servlet-mapping>
</web-app>

 

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>文件上传</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <form method="post" action="FileUpload" ENCTYPE="multipart/form-data">
        文件:<input type="file" name="file">
        <input type="submit" value="上传" name="submit">
    </form>
  </body>
</html>
package servlets;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

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.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUpload extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
        boolean isMultipart = ServletFileUpload.isMultipartContent(req);
        if(isMultipart){
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            Iterator items;
            try {
                items = upload.parseRequest(req).iterator();
                while(items.hasNext()){
                    FileItem item = (FileItem) items.next();
                    if(!item.isFormField()){
                        //取出上传文件的文件名称
                        String name = item.getName();
                        String fileName = name.substring(name.lastIndexOf("\\")+1, name.length());
                        //String path = req.getRealPath("file") + File.separatorChar + fileName;
                        String path = "D:\\" + fileName;
                        //上传文件
                        File uploadedFile = new File(path);
                        item.write(uploadedFile);
                        //打印上传成功信息
                        resp.setContentType("text/html");
                        resp.setCharacterEncoding("gb2312");
                        PrintWriter out = resp.getWriter();
                        out.print("<font size='2'>上传的文件为:"+ name + "<br>");
                        out.print("保存地址为:"+path+"</font>");
                    }
                }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }
}

 

posted @ 2013-09-04 22:11  残星  阅读(515)  评论(0编辑  收藏  举报