html5實(shí)現(xiàn)多文件的上傳示例代碼
來源:易賢網(wǎng) 閱讀:1585 次 日期:2016-07-11 10:50:14
溫馨提示:易賢網(wǎng)小編為您整理了“html5實(shí)現(xiàn)多文件的上傳示例代碼”,方便廣大網(wǎng)友查閱!

多文件的上傳在以前或許實(shí)現(xiàn)起來會(huì)很復(fù)雜,不過自從html5的出現(xiàn),讓其變得非常容易,下面有個(gè)不錯(cuò)的示例,大家可以參考下

主要用到的是<input>的multiple屬性

代碼如下:

<input type="file" name="multipleFileUpload" multiple />

下面是頁面的詳細(xì)代碼:

代碼如下:

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>Solution 4-5: Sending multiple files</title>

</head>

<body>

<form id="myForm"

action="http://10.10.25.31:8080/myupload/UploadPhotoServlet" ENCTYPE="multipart/form-data" METHOD="POST">

<input type="file" name="multipleFileUpload" multiple /> <input

type="submit" value="提交"> <input type="reset" value="重設(shè)">

</form>

</body>

</html>

java后臺(tái)的詳細(xì)代碼:

代碼如下:

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 ();//創(chuàng)建磁盤工廠

factory.setRepository(tmp);//設(shè)置文件緩存路徑

factory.setSizeThreshold(10 * 1096 );//將文件保存在內(nèi)存還是磁盤臨時(shí)文件夾的默認(rèn)臨界值,值為10240,即10kb

ServletFileUpload sfu=new ServletFileUpload(factory);//創(chuàng)建處理工具

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

String fileName=null;

try {

List<FileItem> 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();

}

}

更多信息請查看網(wǎng)頁制作
易賢網(wǎng)手機(jī)網(wǎng)站地址:html5實(shí)現(xiàn)多文件的上傳示例代碼
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)