文件的上传和下载

1.上传:

----在 upload.jsp 页面上使用 jQuery 实现 "新增一个附件", "删除附件". 但至少需要保留一个.

----对文件的扩展名和文件的大小进行验证. 以下的规则是可配置的. 而不是写死在程序中的.
          a.文件的扩展名必须为 .pptx, docx, doc
          b.每个文件的大小不能超过 1 M
          c.总的文件大小不能超过 5 M.
          d.若验证失败, 则在 upload.jsp 页面上显示错误消息:
                  若某一个文件不符合要求: xxx 文件扩展名不合法 或 xxx 文件大小超过 1 M
                 总的文件大小不能超过 5 M.
          e.若验证通过, 则进行文件的上传操作
-----文件上传, 并给一个不能和其他文件重复的名字, 但扩展名不变
-----将文件的一些属性存储在mysql数据库中,在对应的数据表中添加一条记录. id, file_name, file_path, file_desc(对文件的描述)

2.下载:进行文件上传时, 表单需要做的准备:

1). 请求方式为 POST: <form action="uploadServlet" method="post" ... >
2). 使用 file 的表单域: <input type="file" name="file"/>
3). 使用 multipart/form-data (表示表单以二进制的方式进行文件的传输) 的请求编码方式:

 

实现上述功能需要导入多个开源架包:

1.与出入JQuery语句相关的:jquery-1.7.2.js,放在WebContent下的scripts包中;

2.与文件的上传下载相关的:commons-io-2.0.jar,commons-fileupload-1.2.1.jar

3.与连接数据库连接池相关的:c3p0-0.9.1.2.jar,commons-dbutils-1.3.jar,

mysql-connector-java-5.1.7-bin.jar,gson-2.2.4.jar

--------------------------------------------------------------------------------------------

1.首先连接数据库连接池,及将文件的一些属性插入到数据库中的方法...,其方法是通用的每次使用粘贴过来就行了

 

(1)在src目录下建立c3p0-congig.xml文件,连接数据库连接池的一些变量的配置;

?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>

  <named-config name="javawebapp"> 
  
      <property name="user">root</property>
    <property name="password">lxn123</property>
    <property name="jdbcUrl">jdbc:mysql:///test</property>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
  
    <property name="acquireIncrement">2</property>
    <property name="initialPoolSize">5</property>
    <property name="minPoolSize">5</property>
    <property name="maxPoolSize">10</property>

    <property name="maxStatements">20</property> 
    <property name="maxStatementsPerConnection">5</property>

  </named-config>
</c3p0-config>

 

(2)连接 数据连接池的类:JDBCUtils

package com.lanqial.fileupload.app.db;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JDBCUtils {

    private static DataSource dataSource = null;
    
    static{
        dataSource = new ComboPooledDataSource("javawebapp");
    }
    
    public static Connection getConnection(){  
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DBException("");
        }
    }
 
    public static void release(Connection connection) {
        try {
            if(connection != null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DBException("");
        }
    }
    
}

 

(3)DAO类:对数据库的增删改查的方法封装,便于子类继承;

package com.lanqial.fileupload.app.db;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class DAO<T>{

    public static QueryRunner runner = new QueryRunner();
    
    private Class<T> clazz;
    
    public DAO() {
        
        Type type = getClass().getGenericSuperclass();
        
        if(type instanceof ParameterizedType){
            ParameterizedType pt = (ParameterizedType) type;
            
            Type [] parameterArgs = pt.getActualTypeArguments();
            
            if(parameterArgs != null && parameterArgs.length > 0){
                if(parameterArgs[0] instanceof Class){
                    clazz = (Class<T>) parameterArgs[0]; 
                }
            }
        }
        
    }
    
    protected void update(Connection conn, String sql, Object ... args) throws SQLException{
        runner.update(conn, sql, args);
    }
    
    protected T get(Connection conn, String sql, Object ... args) throws SQLException{
        return runner.query(conn, sql, new BeanHandler<>(clazz), args); 
    }
    
    protected List<T> getForList(Connection conn, String sql, Object ... args) throws SQLException{
        return runner.query(conn, sql, new BeanListHandler<>(clazz), args); 
    }
    
    protected <E> E getValue(Connection conn, String sql, Object ... args) throws SQLException{
        E result = null;
        result = (E) runner.query(conn, sql, new ArrayHandler(), args)[0];
        return result;
    }
    
}

 

(4)异常处理类:

package com.lanqial.fileupload.app.db;

public class DBException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public DBException() {
        // TODO Auto-generated constructor stub
    }
    
    public DBException(String msg) {
        super(msg);
    }
    
    public DBException(String msg, Exception ex) {
        super(msg, ex);
    }
}

 

(5)实现增删改查的方法,继承于父类DAO;

package com.lanqial.fileupload.app.db;

import java.sql.Connection;
import java.util.List;

import com.lanqiao.javaweb.fileupload.app.beans.FileUploadBean;

public class UploadFileDao extends DAO<FileUploadBean>{
    
    public List<FileUploadBean> getFiles(){
        
        Connection conn = null;
        
        try {
            conn = JDBCUtils.getConnection();
            String sql = "SELECT id, file_name fileName, file_path filePath, " +
                    "file_desc fileDesc FROM upload_files";
            return getForList(conn, sql);            
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            JDBCUtils.release(conn);
        }
        
        return null;
    } 
    
    public void save(List<FileUploadBean> uploadFiles){
        
        Connection conn = null;
        
        try {
            conn = JDBCUtils.getConnection();
            String sql = "INSERT INTO upload_files (file_name, file_path, file_desc) VALUES " +
                    "(?, ?, ?)";
            for(FileUploadBean file: uploadFiles){
                update(conn, sql, file.getFileName(), file.getFilePath(), file.getFileDesc());
            }            
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            JDBCUtils.release(conn);
        }
        
    }
    
}

 

(5)对数据库中的属性的封装类:

package com.lanqiao.javaweb.fileupload.app.beans;


//数据库中的属性的封装类;
public class FileUploadBean {
    
    private Integer id;//id号
    private String fileName;//文件名
    private String filePath;//文件路径
    private String fileDesc;//文件的描述
    
    public FileUploadBean() {
        super();
    }
    
    public FileUploadBean(String fileName, String filePath, String fileDesc) {
        super();
        this.fileName = fileName;
        this.filePath = filePath;
        this.fileDesc = fileDesc;
    }

    public FileUploadBean(Integer id, String fileName, String filePath, String fileDesc) {
        super();
        this.id = id;
        this.fileName = fileName;
        this.filePath = filePath;
        this.fileDesc = fileDesc;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getFileDesc() {
        return fileDesc;
    }

    public void setFileDesc(String fileDesc) {
        this.fileDesc = fileDesc;
    }
    
    
}

 

2.文件上传和下载的方法及配置和映射的文件

 

(1)在src目录下建立upload.properties的file文件,对要上传的文件的属性做一些要求;

exts=pptx,docx,doc,zip
file.max.size=1048576
total.file.max.size=5242880

 

(2)获取文件upload.properties文件里面数据的方法:

package com.lanqiao.javaweb.fileupload.app.utils;

import java.util.HashMap;
import java.util.Map;

public class FileUploadAppProperties {
    //获取文件upload.properties文件里面数据的方法
    
    private Map<String, String> properties=new HashMap<String, String>();
    
    private FileUploadAppProperties() {}
    
    private static FileUploadAppProperties instance=new FileUploadAppProperties();
    
    public static FileUploadAppProperties getInstance() {
        return instance;
    }
    
    public void addProperty(String propertyName,String propertyValue){
        properties.put(propertyName, propertyValue);
    }
    
    public String getProperty(String propertyName){
        return properties.get(propertyName);
    }
}

 

(3)创建一个监听器类:

package com.lanqiao.javaweb.fileupload.app.listener;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.lanqiao.javaweb.fileupload.app.utils.FileUploadAppProperties;

//监听器:定位到upload.properties文件,
//获取上传文件的类型:exts=pptx,docx,doc,
//上传的每个文件的大小:file.max.size=1048576,
//上传的所有文件总的大小:total.file.max.size=5242880


public class FileUploadAppListener implements ServletContextListener {

    public FileUploadAppListener() {}
    
    //创建监听器的方法;该方法获取src目录下upload.properties文件里的值
    public void contextInitialized(ServletContextEvent arg0)  { 
        //利用反射获取文件里的数据
        InputStream in=getClass().getClassLoader().getResourceAsStream("/upload.properties");
        
        Properties properties=new Properties();
        try {
            properties.load(in);
            for(Map.Entry<Object, Object> prop:properties.entrySet()){
                String propertyName=(String) prop.getKey();
                String propertyValue=(String) prop.getValue();
                //FileUploadAppProperties类的方法
                FileUploadAppProperties.getInstance().addProperty(propertyName, propertyValue);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void contextDestroyed(ServletContextEvent arg0){}

    
}

 

(4)文件上传类的异常处理类:

package com.lanqiao.javaweb.fileupload.app.servlet;

public class InvalidExtNameException extends Exception {
    
    /**
     * 异常类,验证文件类,如果不正确,就抛出异常
     */
    private static final long serialVersionUID = 1L;

    public InvalidExtNameException(String msg) {
        super(msg);
    }
}

 

(5)文件上传的Servlet类:实现文件的上传和将数据插入到数据库中;

package com.lanqiao.javaweb.fileupload.app.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

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;
import org.apache.commons.io.FileUtils;

import com.lanqial.fileupload.app.db.UploadFileDao;
import com.lanqiao.javaweb.fileupload.app.beans.FileUploadBean;
import com.lanqiao.javaweb.fileupload.app.utils.FileUploadAppProperties;

public class FileUploadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    //文件的绝对路径
    private static final String FILE_PATH = "/WEB-INF/files/";
    //存储文件的临时文件夹地址
    private static final String TEMP_DIR = "f:\\tempDirectory";
    
    private UploadFileDao dao = new UploadFileDao();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        //请求的转发,UTF-8,即可以上传汉字
        request.setCharacterEncoding("UTF-8");
        String path=null;
        
        //获取 ServletFileUpload 对象. 在文件里的属性的值
        ServletFileUpload upload=getServletFileUpload();
        try {

            //把需要上传的 FileItem 都放入到该 Map 中
            //键: 文件的待存放的路径, 值: 对应的 FileItem 对象
            Map<String, FileItem> uploadFiles = new HashMap<String, FileItem>();
            
            //解析请求, 得到 FileItem 的集合.
            List<FileItem> items = upload.parseRequest(request);
            
            //1. 构建 FileUploadBean 的集合, 同时填充 uploadFiles
            List<FileUploadBean> beans = buildFileUploadBeans(items, uploadFiles);
            
            //2. 校验扩展名:处理是可能会出现异常,被获取,用path属性处理;
            vaidateExtName(beans);

            //3. 校验文件的大小: 在解析时, 已经校验了, 我们只需要通过异常得到结果. 
            
            //4. 进行文件的上传操作.
            upload(uploadFiles);
            
            //5. 把上传的信息保存到数据库中
            saveBeans(beans);
            
            //6. 删除临时文件夹的临时文件FileUtils.delAllFile(TEMP_DIR);
            //FileUtils.delAllFile(TEMP_DIR);
            
            //文件的扩展名合法,就。。。
            path = "/app/success.jsp";
            
        } catch (Exception e) {
            
            //文件的扩展名不合法就。。。
            path="/app/upload.jsp";
            request.setAttribute("message", e.getMessage());
        }
        //在jsp中信息的显示
        request.getRequestDispatcher(path).forward(request, response);
        
    }
    
    //把上传的信息保存到数据库中
    private void saveBeans(List<FileUploadBean> beans) {
        dao.save(beans); 
        
    }
    
    // 文件上传前的准备工作. 得到 filePath 和 InputStream
    private void upload(Map<String, FileItem> uploadFiles) throws IOException {
        for(Map.Entry<String, FileItem> uploadFile: uploadFiles.entrySet()){
            
            FileItem item = uploadFile.getValue();
            //获取所要上传的文件的路径
            String filePath=item.getName();
            
            upload(filePath, item.getInputStream());
        }
    }
    
    //文件上传的 IO 方法.
    private void upload(String fileName, InputStream in) throws IOException {
        //截取要上传的文件名
        fileName=fileName.substring(3);
        
        //将文件上传至:f:\\files
        fileName = "f:\\files\\" + fileName;
        
        OutputStream out = new FileOutputStream(fileName);
        
        byte [] buffer = new byte[1024];
        int len = 0;
        while((len =in.read(buffer)) != -1){
            out.write(buffer, 0, len);
        }
        out.close();
        System.out.println(fileName); 
    }
    
    //校验扩展名:
    private void vaidateExtName(List<FileUploadBean> beans) throws Exception {
        String exts = FileUploadAppProperties.getInstance().getProperty("exts");
        List<String> extList = Arrays.asList(exts.split(","));
        System.out.println(extList);
        
        for(FileUploadBean bean: beans){
            String fileName = bean.getFileName();
            System.out.println(fileName.indexOf(".")); 
            
            String extName = fileName.substring(fileName.lastIndexOf(".") + 1);
            System.out.println(extName); 
            
            if(!extList.contains(extName)){
                //InvalidExtNameException异常类,文件的扩展名不合法,就抛出异常
                throw new InvalidExtNameException(fileName + "文件的扩展名不合法");
            }
        }
        
    }

    /*
     *利用传入的 FileItem 的集合, 构建 FileUploadBean 的集合, 同时填充 uploadFiles
     
     *FileUploadBean 对象封装了: id, fileName, filePath, fileDesc
     *uploadFiles: Map<String, FileItem> 类型, 存放文件域类型的  FileItem. 键: 待保存的文件的名字 ,值: FileItem 对象
     *
     *构建过程:
     *1. 对传入 FileItem 的集合进行遍历. 得到 desc 的那个 Map. 键: desc 的 fieldName(desc1, desc2 ...). 
     *值: desc 的那个输入的文本值
     *
     *2. 对传入 FileItem 的集合进行遍历. 得到文件域的那些 FileItem 对象, 构建对应的 key (desc1 ....) 来获取其 desc.
     *构建的 FileUploadBean 对象, 并填充 beans 和 uploadFiles
    */
     
    // 构建 FileUploadBean 的集合, 同时填充 uploadFiles,文件与文件的描述配对
    private List<FileUploadBean> buildFileUploadBeans
        (List<FileItem> items, Map<String, FileItem> uploadFiles) throws Exception {
        
        List<FileUploadBean> beans = new ArrayList<FileUploadBean>();
        
        //1.遍历FileItem的集合,得到desc的Map<String, String>,其中键fileName(desc1,desc2...)
        //值是表单域要上传的文件
        Map<String, String> descs = new HashMap<String, String>();
        
        for(int i = 0; i < items.size(); i++){
            FileItem item = items.get(i);
            
            if(item.isFormField()){
                //desc1 或 desc2 ...
                String fieldName = item.getFieldName();
                String desc = item.getString("UTF-8");
                
                descs.put(fieldName, desc);
            }
        }
        //2.在遍历FileItam的集合,得到文件域的FileItem对象,
        //每得到一个FileItem对象都会创建一个fileuploadBean对象
        //得到fileName,构建filePath,从1的Map中得到当前的FileItem对用的desc
        //用fileName后面的数字去匹配
        for(int i = 0; i < items.size(); i++){
            FileItem item = items.get(i);
            FileUploadBean bean = null;
            if(!item.isFormField()){
                String fieldName = item.getFieldName();
                String descName = "desc" + fieldName.substring(fieldName.length() - 1);
                String desc = descs.get(descName); 
                
                //对应文件名
                String fileName = item.getName();
                String filePath = getFilePath(fileName);
                
                bean = new FileUploadBean(fileName, filePath, desc);
                beans.add(bean);
                
                uploadFiles.put(bean.getFilePath(), item);
            }            
        }
        
        return beans;
    }
    
    /*获取路径名的方法:
     * 根据跟定的文件名构建一个随机的文件名
     * 1. 构建的文件的文件名的扩展名和给定的文件的扩展名一致
     * 2. 利用 ServletContext 的 getRealPath 方法获取的绝对路径
     * 3. 利用了 Random 和 当前的系统时间构建随机的文件的名字
     * */
    private String getFilePath(String fileName) {
        //获取文件的扩展名
        String extName = fileName.substring(fileName.lastIndexOf("."));
        Random random = new Random();
        //文件的绝对路径,设置并得到,设置文件的名字,防止文件重名
        String filePath = getServletContext().getRealPath(FILE_PATH) + "\\" + System.currentTimeMillis() + random.nextInt(100000) + extName;
        return filePath;
    }

    /*
     * 构建ServletFileUpload对象
     * 从配置文件中读取了部分属性,为用户上传文件进行约束
     * 该方法代码是从文档中得到的
     * */
    private ServletFileUpload getServletFileUpload(){
        //上传文件的类型
        String exis=
                FileUploadAppProperties.getInstance().getProperty("pptx,docx,doc");
        //上传的每个文件的大小
        String fileMaxSize=
                FileUploadAppProperties.getInstance().getProperty("file.max.size");
        //上传的所有文件的总大小
        String totalFileMaxSize=
                FileUploadAppProperties.getInstance().getProperty("total.file.max.size");
        //获取表单里须上传的文件
        DiskFileItemFactory factory = new DiskFileItemFactory();
        
        // Set factory constraints,设置文件的大小
        factory.setSizeThreshold(1024 * 500);
        
        //超过要求上传文件的大小时,上传的临时文件夹,但是在这儿并没有什么用,
        //因为,已经设置了文件的的大小,如果文件太大,会发出提示,不予上传
        File tempDirectory = new File(TEMP_DIR);
        
        factory.setRepository(tempDirectory);
        
        System.out.println(tempDirectory);
        
        ServletFileUpload upload = new ServletFileUpload(factory);
        
        upload.setSizeMax(Integer.parseInt(totalFileMaxSize));
        upload.setFileSizeMax(Integer.parseInt(fileMaxSize));
        
        return upload;
    }
}

 

(6)文件下载的Servlet类:

package com.lanqiao.javaweb.filedownloadservlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLClassLoader;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.net.URLCanonicalizer;

public class FiledownloadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        //response.setContentType("application/x-msdownload"):
        //即设置一个响应的类型: application/x-msdownload
        //通知客户端浏览器: 这是一个需要下载的文件, 不能再按普通的 html 的方式打开.
        response.setContentType("application/x-msdownload");
        
        //response.setHeader("Content-Disposition", "attachment;filename=abc.txt");
        //设置用户处理的方式: 响应头: Content-Disposition
        //通知客户端浏览器: 不再有浏览器来处理该文件, 而是交由用户自行处理
        
        //.txt为下载文件的类型,必须与后面downloadFileName的类型相同;
        String fileName="文件下载.txt";
        
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        
        //读取进来然后下载;
        
        OutputStream out=response.getOutputStream();
        //所读取文件的地址,及类型
        String downloadFileName="C:\\hh.txt";
        
        //读取要下载的文件
        InputStream in=new FileInputStream(downloadFileName);
        
        byte[] buffer=new byte[1024];
        int len=0;
        while((len=in.read(buffer))!=-1){
            out.write(buffer, 0, len);
        }
        
        in.close();
    }

}

 

(7)web.xml文件中映射和配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">
 <display-name>day-14-fileupload</display-name>
  <servlet>
    <description></description>
    <display-name>UploadServlet</display-name>
    <servlet-name>UploadServlet</servlet-name>
    <servlet-class>com.lanqiao.javaweb.UploadServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>UploadServlet</servlet-name>
    <url-pattern>/fileupload/uploadServlet</url-pattern>
  </servlet-mapping>
  
  <listener>
    <listener-class>com.lanqiao.javaweb.fileupload.app.listener.FileUploadAppListener</listener-class>
  </listener>
  
  <servlet>
    <description></description>
    <display-name>FileUploadServlet</display-name>
    <servlet-name>FileUploadServlet</servlet-name>
    <servlet-class>com.lanqiao.javaweb.fileupload.app.servlet.FileUploadServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>FileUploadServlet</servlet-name>
    <url-pattern>/app/fileUploadServlet</url-pattern>
  </servlet-mapping>
  
  
  <servlet>
    <description></description>
    <display-name>FiledownloadServlet</display-name>
    <servlet-name>FiledownloadServlet</servlet-name>
    <servlet-class>com.lanqiao.javaweb.filedownloadservlet.FiledownloadServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>FiledownloadServlet</servlet-name>
    <url-pattern>/filedownloadServlet</url-pattern>
  </servlet-mapping>
</web-app>

 

3.jsp页面:

 

(1)进行文件上传和下载选择的jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    
    <a href="app/upload.jsp"><h3>上传文件...</h3></a>
    
    <a href="download.jsp"><h3>文件下载...</h3></a>
    
</body>
</html>

 

(2)文件上传的app文件夹里的jsp:upload.jsp,success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/scripts/jquery-1.7.2.js"></script>
<script type="text/javascript">
    
    $(function(){
        
        var i = 2;
        
        //获取id为addFile,并为其添加click相应函数;
        $("#addFile").click(function(){
            $(this).parent().parent().before("<tr class='file'><td>File" 
                    + i + ":</td><td><input type='file' name='file" 
                    + i + "'/></td></tr>"
                    + "<tr class='desc'><td>Desc" 
                    + i + ":</td><td><input type='text' name='desc" 
                    + i + "'/><button id='delete" 
                    + i + "'>删除</button></td></tr>");
            i++;
            
            //获取新添加的删除按钮,获取id为delete
            $("#delete" + (i-1)).click(function(){
                var $tr = $(this).parent().parent();
                $tr.prev("tr").remove();
                $tr.remove();
                
                //对 i 重写排序
                $(".file").each(function(index){
                    var n = index + 1;
                    
                    $(this).find("td:first").text("File" + n);
                    $(this).find("td:last input").attr("name", "file" + n);
                });
                
                $(".desc").each(function(index){
                    var n = index + 1;
                    
                    $(this).find("td:first").text("Desc" + n);
                    $(this).find("td:last input").attr("name", "desc" + n);
                });
                
                i = i - 1;
            });
            
            return false;
        });
        
    });
    
</script>

</head>
<body>

    <font color="red">${message }</font>
    <br><br>

    
    <form action="fileUploadServlet" method="post" enctype="multipart/form-data">
        
        <table>
            <tr class="file">
                <td>File1:</td>
                <td><input type="file" name="file1"/></td>
            </tr>
            <tr class="desc">
                <td>Desc1:</td>
                <td><input type="text" name="desc1"/></td>
            </tr>
            
            <tr>
                <td><input type="submit" id="submit" value="提交"/></td>
                <td><button id="addFile">新增一个附件</button></td>
            </tr>
        </table>
        
    </form>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h2>文件上传成功!!!</h2>
    <a href="upload.jsp">Return upload...</a>
</body>
</html>

 

(3)文件下载的jsp:download.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    
    <!-- 静态下载 -->
    <a href="xyz.txt">download xyz.txt</a>
    
    <br><br>
    
    <a href="test.jsp">download test.jsp</a>
    
    <br><br>
    <!-- 动态下载 -->
    <a href="filedownloadServlet">Download To File</a>
    
    
</body>
</html>

 

posted @ 2016-09-05 21:13  G-&-D  阅读(1420)  评论(0编辑  收藏  举报