html5多文件上传 java_HTML5实现多文件的上传示例代码

[转自] http://www.jb51.net/html5/136791.html

主要用到的是的multiple属性

代码如下:

下面是页面的详细代码:

代码如下:

Solution 4-5: Sending multiple files

action="http://10.10.25.31:8080/myupload/UploadPhotoServlet"

ENCTYPE="multipart/form-data" METHOD="POST">

type="submit" value="提交">

java后台的详细代码:

代码如下:

import java.io.File;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.List;

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.FileUploadException;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**

* Servlet implementation class UploadPhotoServlet

*/

public class UploadPhotoServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()

*/

public UploadPhotoServlet() {

super();

// TODO Auto-generated constructor stub

}

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse

* response)

*/

protected void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doPost(request, response);

}

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse

* response)

*/

@SuppressWarnings("unchecked")

protected void doPost(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

String imagePath = "c:\\uploadFile\\Image\\" + getEachDate() + "\\";// 按日期生成文件夹

File uploadPath = new File(imagePath);

if (!uploadPath.exists()) {

uploadPath.mkdirs();

}

File tmp = new File("c:\\tmp\\");

if (!tmp.exists()) {

tmp.mkdirs();

}

DiskFileItemFactory factory = new DiskFileItemFactory();// 创建磁盘工厂

factory.setRepository(tmp);// 设置文件缓存路径

factory.setSizeThreshold(10 * 1096);// 将文件保存在内存还是磁盘临时文件夹的默认临界值,值为10240,即10kb

ServletFileUpload sfu = new ServletFileUpload(factory);// 创建处理工具

sfu.setSizeMax(10 * 1024 * 1024);// 服务器端可以接收的最大文件大小,-1表示无上限

String fileName = null;

try {

List list = sfu.parseRequest(request);// 解析

if (list.size() < 1) {

return;

}

for (int j = 0; j < list.size(); j++) {

FileItem item = list.get(j);

fileName = item.getName();

if (fileName.equals("")) {

request.getRequestDispatcher("/com/visualizerPhoto.jsp")

.forward(request, response);

return;

}

int pos = fileName.lastIndexOf(".");// 取图片文件格式

if (pos > 0) {

Date date = new Date();

fileName = imagePath + date.getTime()

+ fileName.substring(pos);

}

System.out.println("item:" + item);

item.write(new File(fileName));// 写到磁盘

}

} catch (FileUploadException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

// 13-11-15

public static String getEachDate() {

Calendar cal = Calendar.getInstance();

cal.add(Calendar.DATE, 0);

String yesterday = new SimpleDateFormat("yyyy-MM-dd ").format(cal

.getTime());

String[] dates = yesterday.split("-");

String realDate = dates[0].substring(2, 4) + "-" + dates[1] + "-"

+ dates[2];

return realDate.trim();

}

}

下面是效果图:

2d1b5ce36d06f001e5510e7c60510ea1.gif

参与评论 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:1024 设计师:我叫白小胖 返回首页

打赏作者

要努力的子健

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值