java-web文件上传下载

一、基于表单的文件上传:
    1、如果在表单中使用表单元素 <input  type=“file” />,浏览器在解析表单时,会自动生成一个输入框和一个按钮,输入框可供用户填写本地文件的文件名和路径名,按钮可以让浏览器打开一个文件选择框供用户选择文件:
<form action="upload01.jsp" method="post" enctype="multipart/form-data">
     图片路径: <input type="file" name="filepath" />
     图片名称: <input type="text" name="filename" />
     <input type="submit" value="上传"/>
</form>


   2、 Enctype 属性:
         当表单需要上传文件时,需指定表单 enctype 的值为 multipart/form-data,在 form 元素的语法中,enctype 属性指定将数据发送到服务器时浏览器使用的编码类型。enctype 属性取值:
         application/x-www-form-urlencoded:表单 enctype 属性的默认值。这种编码方案使用有限的字符集,当使用了非字母和数字时,必须用”%HH”代替(H 代表十六进制数字)。对于大容量的二进制数据或包含非 ASCII 字符的文本来说,这种编码不能满足要求。
         multipart/form-data:form 设定了enctype=“multipart/form-data”属性后,表示表单以二进制传输数据。

二、Commons-fileupload 组件
    Commons-fileupload 组件是 Apache 开源代码组织用来处理表单文件上传的一个子项目,该组件性能优异,可以支持任意大小的文件的上传。
    Commons-fileupload 组件从 1.1 版本开始依赖 Apache 的另一个项目:commons-io.

1、Commons-fileupload 组件上传的基本原理 :
    FileUpload组件将页面提交的所有元素(普通form表单域,如text和文件域file)都看作一样的FileItem,这样上传页面提交的 request请求也就是一个FileItem的有序组合,FileUpload组件可以解析该request,并返回一个一个的FileItem。而对每一个FileItem,FileUpload组件可以判断出它是普通form表单域还是文件file域,从而根据不同的类型,采取不同的操作--如果是表单域,就读出其值,如果是文件域,就保存文件到服务器硬盘上或者内存中。

2、API:
  在 Commons-fileupload 组件中,主要用到以下三个接口和类:
    org.apache.commons.fileupload.FileItem;
    org.apache.commons.fileupload.disk.DiskFileItemFactory;
    org.apache.commons.fileupload.servlet.ServletFileUpload
    ServletFileUpload 负责处理上传的文件数据,并将每部分的数据封装到 FileItem 对象中。
    DiskFileItemFactory 是创建 FileItem 对象的工厂,在这个工厂类中可以配置内存缓冲区大小和存放临时文件的目录。
    ServletFileUpload 在接收上传文件数据时,会将内容保存到内存缓存区中,如果文件内容超过了 DiskFileItemFactory 指定的缓冲区的大小,那么文件将被保存到磁盘上,存储为 DiskFileItemFactory 指定目录中的临时文件。等文件数据都接收完后,ServletUpload 在从文件中将数据写入到上传文件目录下的文件中.

// >>1. 判断是否是上传表单
		isMultipartContent = ServletFileUpload.isMultipartContent(request);
		if (isMultipartContent) {

			// 读取web.xml中的配置内容
			String allowUploadFileString = super.getSession()
					.getServletContext().getInitParameter("allowUploadFile");

			// >>2. 创建一个解析上传请求的工厂
			// 用于上传的最大内存为10BM,如果超出了最大的内存将会保持在d:/temp
			DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(
					1024 * 1024 * 10, new File("D:/temp"));
			ServletFileUpload fileUpload = new ServletFileUpload(
					diskFileItemFactory);
			fileUpload.setFileSizeMax(1024 * 1024 * 8); // 设置单个上传表单元素的最大值
			fileUpload.setSizeMax(1024 * 1024 * 60);// 设置这个上传表单的最大值

			try {
				// >>3. 将请求中的表单元素解析成对应的fileitem. 每一个表单元素对应的一个fileitem
				List<FileItem> fileItems = fileUpload.parseRequest(request);

				// >>4.处理普通表单元素和上传表单元素
				for (FileItem fileItem : fileItems) {
					if (fileItem.isFormField()) { // 处理普通表单元素
						textMap.put(fileItem.getFieldName(),
								fileItem.getString("UTF-8"));
					} else {
						// 当配置了后缀名后才进行后缀名的判断
						if (allowUploadFileString != null
								&& !ArrayUtils.contains(allowUploadFileString
										.split(","), FilenameUtils
										.getExtension(fileItem.getName()))) {
							throw new LogicException("上传的文件不合法...");
						}

						// 处理上传表单元素
						if (fileItem.getSize() > 0) {
							fileMap.put(fileItem.getFieldName(), fileItem);
						}
					}
				}
			} catch (FileSizeLimitExceededException e) {
				throw new LogicException("上传单个文件超过了指定的大小");
			} catch (SizeLimitExceededException e) {
				throw new LogicException("上传整个表单的大小超过了指定的大小");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (FileUploadException e) {
				e.printStackTrace();
			}
		}


3、文件上传的乱码问题解决:
    //如果Item是表单字段
    if (item.isFormField()) {
        String name = item.getFieldName();
        String value = item.getString("UTF-8") ;  
        System.out.println("^^^^"+name +"="+value);
    }

三、文件的下载:
    1、情景:在一些网络系统中,需要隐藏下载文件的真实地址,或者下载的文件需要一个程序来动态的确定后在传送给客户端
    方案:利用程序编码实现下载:
       可以增加安全访问控制,只对经过授权认证的用户提供下载
       可以从任意位置提供下载的数据

    2、利用程序实现下载需要设置 2 个报头:
      Web 服务器需要告诉浏览器其所输出的内容的类型不是普通的文本文件或 HTML 文件,而是一个要保存到本地的下载文件。设置Content-Type 的值为:application/x-msdownload,Web 服务器希望浏览器不直接处理相应的实体内容,而是由用户选择将相应的实体内容保存到一个文件中,这需要设置 Content-Disposition 报头。该报头指定了接收程序处理数据内容的方式,在 HTTP 应用中只有 attachment 是标准方式,attachment 表示要求用户干预。在 attachment 后面还可以指定 filename 参数,该参数是服务器建议浏览器将实体内容保存到文件中的文件名称。在设置 Content-Dispostion 之前一定要指定 Content-Type.
    /* 
     *设置响应报头 contentType: application/x-msdownload --> 
     *告诉浏览器其所输出的内容的类型不是普通的文本文件或 HTML 文件,
     *而是一个要保存到本地的下载文件
     */
     //response.setHeader("content-type", "application/x-msdownload");
     response.setContentType("application/x-msdownload");
     /*
      *  设置响应抱头 Content-Disposition: attachment
      *  Web 服务器希望浏览器不直接处理相应的实体内容,
      *  而是由用户选择将相应的实体内容保存到一个文件中
      */ 
   response.setHeader("Content-Disposition", "attachment; filename=a.jpg");


    3、文件的输出:
       因为要下载的文件可以是各种类型的文件,所以要将文件传送给客户端,其相应内容应该被当做二进制来处理,所以应该调用response.getOutputStream()方法返回 OutputStream 对象来向客户端写入文件内容。  
 //获取响应输出流
 OutputStream os=response.getOutputStream();
 BufferedOutputStream bos=new BufferedOutputStream(os);
 FileInputStream fis=new FileInputStream("D:\\haha.jpg");
 BufferedInputStream bis=new BufferedInputStream(fis);
 byte[] tem=new byte[1024*10];
 int len=0; 
 while((len=bis.read(tem))!=-1){
      bos.write(tem, 0, len);
 }
 bos.close();
 bis.close();
      
 
    4、下载文件名的乱码问题:      
response.setHeader("Content-Disposition", "attachment; filename="
				+ new String(user.getFileName().getBytes("GBK"), "ISO-8859-1"));



完整示例代码:
package cn.itcast.cd.domain;

public class User {
	private Long id;
	private String name;
	private Boolean sex;
	private String pic;
	private String ip;
	private String fileName;
	private String filePath;
	private long fileSize;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Boolean getSex() {
		return sex;
	}

	public void setSex(Boolean sex) {
		this.sex = sex;
	}

	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 Long getId() {
		return id;
	}

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

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getPic() {
		return pic;
	}

	public void setPic(String pic) {
		this.pic = pic;
	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", pic="
				+ pic + ", ip=" + ip + ", fileName=" + fileName + ", filePath="
				+ filePath + ", fileSize=" + fileSize + "]";
	}
}

package cn.itcast.cd.dao;

import java.util.List;

import cn.itcast.cd.domain.User;

public interface IUserDAO {
	void add(User user);
	List<User> list();
	User get(Long id);
}


package cn.itcast.cd.daoImpl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import cn.itcast.cd.Utils.Utils;

public class BaseDao {
	// 使用DbUtils中的QueryRunner对象.
	private QueryRunner queryRunner = new QueryRunner(Utils.getDataSource());
	/*
	 * 执行修改
	 */
	public void exeUpdate(String sql, Object... params) {
		try {
			queryRunner.update(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/*
	 * 执行查询
	 */
	public <T> T exeQuery(String sql, ResultSetHandler<T> rsh, Object... params) {
		try {
			return queryRunner.query(sql, rsh, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}

package cn.itcast.cd.daoImpl;

import java.util.List;

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

import cn.itcast.cd.dao.IUserDAO;
import cn.itcast.cd.domain.User;

public class UserDAOImpl extends BaseDao implements IUserDAO {

	@Override
	public void add(User user) {
		String sql = "insert into user values(null, ?, ?, ?, ?, ?, ?)";
		Object[] params = { user.getName(), user.getSex(), user.getPic(), user.getIp(),
				user.getFileName(), user.getFilePath() };
		exeUpdate(sql, params);
	}

	@Override
	public List<User> list() {
		String sql = "select * from user";
		return exeQuery(sql, new BeanListHandler<User>(User.class));
	}

	@Override
	public User get(Long id) {
		String sql = "select * from user where id=?";
		return exeQuery(sql, new BeanHandler<User>(User.class), id);
	}
}


package cn.itcast.cd.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

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

import org.apache.commons.lang3.StringUtils;

public class BaseServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String mode = req.getParameter("mode");

		if (StringUtils.isNotBlank(mode)) {
			try {
				Method method = this.getClass().getMethod(mode,
						HttpServletRequest.class, HttpServletResponse.class);
				method.invoke(this, req, resp);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			doMethod(req, resp); // 默认调用
		}
	}

	protected void doMethod(HttpServletRequest req, HttpServletResponse resp) {

	}
}

package cn.itcast.cd.servlet;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;

import cn.itcast.cd.Utils.MyException;
import cn.itcast.cd.Utils.UploadFileInfo;

public class MyHttpServletRequest extends HttpServletRequestWrapper {
	// 装在普通表单元素的map
	private Map<String, String> formFiledMap = new HashMap<String, String>();

	// 装在上传表单元素的Map
	private Map<String, FileItem> uploadFieldMap = new HashMap<String, FileItem>();

	Boolean isMultipartContent = false;

	public MyHttpServletRequest(HttpServletRequest request) {
		super(request);
		// 检查是否是文件上传请求
		isMultipartContent = ServletFileUpload.isMultipartContent(request);
		if (isMultipartContent) {
			// 创建工程,设置上传最大的内存空间和临时文件存放位置.
			FileItemFactory factory = new DiskFileItemFactory(1024 * 1024 * 10,
					new File("D:/tmp"));
			ServletFileUpload fileUpload = new ServletFileUpload(factory);
			fileUpload.setFileSizeMax(1024 * 1024); // 设置单个文件上传的最大值为1M.
			fileUpload.setSizeMax(1024 * 1024 * 5); // 设置整个表单的文件上传最大值为5M.
			try {
				List<FileItem> fileItems = fileUpload.parseRequest(request);
				for (FileItem fileItem : fileItems) {
					// 是否是普通表单元素,即type不为file的表单元素
					if (fileItem.isFormField()) {
						formFiledMap.put(fileItem.getFieldName(),
								fileItem.getString("UTF-8"));
					} else {
						if (fileItem.getSize() > 0) {
							uploadFieldMap.put(fileItem.getFieldName(),
									fileItem);
						}
					}
				}
			} catch (SizeLimitExceededException e) {
				throw new MyException("上传文件的大小超过限制!");
			} catch (FileUploadException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
	}

	// 包装原来的方法
	@Override
	public String getParameter(String name) {
		// 是上传文件的表单,要得到参数值,直接根据name,取存在map中的值.
		if (isMultipartContent) {
			return formFiledMap.get(name);
		} else {
			return super.getParameter(name);
		}
	}

	// 已经将Map修改了,覆盖原来的方法
	@Override
	public Map getParameterMap() {
		if (isMultipartContent) {
			return formFiledMap;
		} else {
			return super.getParameterMap();
		}
	}

	/**
	 * 上传表单中指定表单元素中的文件.
	 * 
	 * @param formFieldName
	 *            表单中元素的名称
	 * @param tagPath
	 *            上传的目标目录,即服务器上存放上传文件的目录.
	 * 
	 * @return 由于上传成功后,我们需要将上传的文件名和文件地址存放到数据库,
	 *         也即需要返回给Servlet上传的文件信息,故将文件信息封装成一个对象返回.
	 * 
	 */
	public UploadFileInfo upload(String formFieldName, String tagPath) {
		FileItem fileItem = uploadFieldMap.get(formFieldName);
		if (fileItem != null) {
			// 得到文件名fileItem.getName()有些浏览器可能得到的是全路径,而这里只需要文件名
			String srcFileName = FilenameUtils.getName(fileItem.getName()); 
			long fileSize = fileItem.getSize();

			//不同的用户可能上传相同的文件名,不处理就被覆盖,所以这里需要用唯一标示进行文件区分.
			String tagFilePath = tagPath + "/" + UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(srcFileName); 
			
			if (!tagPath.startsWith("/")){
				tagPath = "/" + tagPath;
			}
			
			//判断目录是否存在,不存在就创建
			File dir = new File(getSession().getServletContext().getRealPath("/") + tagPath);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			
			//写到指定目录
			try {
				fileItem.write(new File(getSession().getServletContext().getRealPath("/"), tagFilePath));
			} catch (Exception e) {
				e.printStackTrace();
			}
			return new UploadFileInfo(srcFileName, tagFilePath, fileSize);		
		}
		return null;
	}
}

package cn.itcast.cd.servlet;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

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

import org.apache.catalina.startup.SetAllPropertiesRule;
import org.apache.commons.lang3.StringUtils;

import cn.itcast.cd.Utils.MyException;
import cn.itcast.cd.Utils.UploadFileInfo;
import cn.itcast.cd.dao.IUserDAO;
import cn.itcast.cd.daoImpl.UserDAOImpl;
import cn.itcast.cd.domain.User;

/**
 * Servlet implementation class UploadServlet
 */
public class UploadServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	IUserDAO dao = null;

	@Override
	public void init() throws ServletException {
		dao = new UserDAOImpl();
	}

	public void add(HttpServletRequest request, HttpServletResponse response)
			throws InterruptedException {
		// request不能接收上传文件的表单参数,返回全部为null,所以用MyHttpServletRequest类进行包装,单独处理.

		// Thread.sleep(1000);

		MyHttpServletRequest myRequest = null;
		try {
			myRequest = new MyHttpServletRequest(request);
		} catch (MyException e) { // 捕捉自定义异常,提示用户上传文件的大小限制
			request.setAttribute("error", e.getMessage());
			upload(request, response);
			return;
		}

		/*
		 * 从页面得到标示信息,由于是文件上传的请求,必须用myRequest
		 */
		String randomName = myRequest.getParameter("randomName");
		String randomValue = myRequest.getParameter("randomValue");
		//session中的标示.
		String randomValueInSession = (String) myRequest.getSession().getAttribute(randomName);
		
//		System.out.println(randomName + " " + randomValue + " " + randomValueInSession);
		
		if(StringUtils.isNotBlank(randomValueInSession) && 
					StringUtils.isNotBlank(randomValue) && StringUtils.isNotBlank(randomName)
					&& randomValueInSession.equals(randomValue)){
			// 接收普通表单元素的参数信息
			String name = myRequest.getParameter("name");
			String sex = myRequest.getParameter("sex");

			// 封装对象
			User user = new User();
			user.setName(name);
			user.setSex(Boolean.parseBoolean(sex));

			String ip = request.getRemoteAddr();
			user.setIp(ip);

			// 上传头像信息
			UploadFileInfo picInfo = myRequest.upload("pic", "/headPic");
			if (picInfo != null) {
				user.setPic(picInfo.getTagFilePath());
			}

			UploadFileInfo fileInfo = myRequest.upload("fileName",
					"/WEB-INF/resource");
			if (fileInfo != null) {
				user.setFilePath(fileInfo.getTagFilePath());
				user.setFileName(fileInfo.getSrcFileName());
				user.setFileSize(fileInfo.getFileSize());
			}

			dao.add(user);

			//提交完成移除session中设置的随机标示
			request.getSession().removeAttribute(randomName);
		} else {
			request.setAttribute("error", "重复提交请求……");
		}	
		list(request, response);
	}

	public void list(HttpServletRequest request, HttpServletResponse response) {
		List<User> list = dao.list();
		request.setAttribute("list", list);
		try {
			request.getRequestDispatcher("/WEB-INF/jsp/list.jsp").forward(
					request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void upload(HttpServletRequest request, HttpServletResponse response) {
		try {
			//防止重复提交
			String randomName = UUID.randomUUID().toString();
			String randomValue = UUID.randomUUID().toString();
			
			request.setAttribute("randomName", randomName );
			request.setAttribute("randomValue", randomValue);
			//当前产生的标示放到session.
			request.getSession().setAttribute(randomName, randomValue);
			
			request.getRequestDispatcher("/WEB-INF/jsp/upload.jsp").forward(
					request, response);
			
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doMethod(HttpServletRequest request,
			HttpServletResponse response) {
		list(request, response);
	}

	public void download(HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
		User user = dao.get(Long.parseLong(id));

		// 发送下载请求给浏览器
		response.setContentType("application/x-msdownload");
		try {
			// 解决下载时文件名乱码问题.
			response.setHeader("Content-Disposition", "attachment;filename="
					+ URLEncoder.encode(user.getFileName(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		BufferedOutputStream bos = null;
		BufferedInputStream bis = null;
		try {
			bos = new BufferedOutputStream(response.getOutputStream());
			bis = new BufferedInputStream(new FileInputStream(new File(
					getServletContext().getRealPath("/"), user.getFilePath())));

			byte[] b = new byte[1024];

			int lenth = 0;
			while ((lenth = bis.read(b)) != -1) {
				bos.write(b, 0, lenth);
				bos.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bos.close();
				bis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


package cn.itcast.cd.Utils;

public class MyException extends RuntimeException {
	public MyException(String message){
		super(message);
	}
}

package cn.itcast.cd.Utils;

public class UploadFileInfo {
	private String srcFileName;
	private String tagFilePath;
	private long fileSize;
	public String getSrcFileName() {
		return srcFileName;
	}
	public void setSrcFileName(String srcFileName) {
		this.srcFileName = srcFileName;
	}
	public String getTagFilePath() {
		return tagFilePath;
	}
	public void setTagFilePath(String tagFilePath) {
		this.tagFilePath = tagFilePath;
	}
	public long getFileSize() {
		return fileSize;
	}
	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}
	public UploadFileInfo(String srcFileName, String tagFilePath, long fileSize) {
		super();
		this.srcFileName = srcFileName;
		this.tagFilePath = tagFilePath;
		this.fileSize = fileSize;
	}
}

package cn.itcast.cd.Utils;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

public class Utils {
	private static DataSource dataSource;
	
	static{
		Properties properties = new Properties();
		try {
			properties.load(Utils.class.getClassLoader().getResourceAsStream("jdbc.properties"));
			dataSource = BasicDataSourceFactory.createDataSource(properties);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static DataSource getDataSource(){
		return dataSource;
	}
}


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>上传文件</title>
</head>
<body>
	<font color="red" size="3px"><b>${error}</b></font>
	<table border="1" width="60%">
		<tr>
			<th>用户名</th>
			<th>性别</th>
			<th>头像</th>
			<th>IP地址</th>
			<th>上传文件名</th>
			<th>操作</th>
		</tr>
		<c:forEach items="${list}" var="user">
			<tr>
				<td>${user.name}</td>
				<td>${user.sex==true?"男":"女"}</td>
				<td><img src="${user.pic}" height="120"></td>
				<td>${user.ip}</td>
				<td>${user.fileName}</td>
				<td><a href="/upload?mode=download&id=${user.id}">下载</a></td>
			</tr>
		</c:forEach>
	</table>
	<form action="/upload?mode=upload" method="post">
		<input type="submit" value="去上传..."/>
	</form>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>上传文件</title>
<script type="text/javascript">
	var tag = true;
	function go(){
		if(tag){
			document.forms[0].submit();
			tag = false;
		}else{
			alert("正在提交中,请勿重复提交请求……");
		}
	}
</script>
</head>
<body>
	<font color="red" size="3px"><b>${error}</b></font>
	<form action="/upload?mode=add" method="post" enctype="multipart/form-data">
		<input type="hidden" name="randomName" value="${randomName}"/>
		<input type="hidden" name="randomValue" value="${randomValue}"/>
		<table>
			<tr>
				 <td>用户名:</td>
				 <td><input type="text" name="name"/></td>
			</tr>	
			<tr>
				 <td>性&emsp;别:</td>
				 <td><input type="radio" name="sex" value="true"/>男
				 	 <input type="radio" name="sex" value="false"/>女
				 </td>
			</tr>	
			<tr>
				 <td>头&emsp;像:</td>
				 <td><input type="file" name="pic"/></td>
			</tr>	
			<!-- <tr>
				 <td>IP地址:</td>
				 <td><input type="text" name="ip"/></td>
			</tr> -->	
			<tr>
				 <td>文件名:</td>
				 <td><input type="file" name="fileName"/></td>
			</tr>	
			<tr>
				<!-- <td colspan="2" align="center"><input type="button" value="提交" onclick="go()"/></td> -->
				<td colspan="2" align="center"><input type="submit" value="提交""/>
			</tr>
		</table>
	</form>
</body>
</html>

你可能感兴趣的:(java,html,Web,上传下载、重复提交)