名站网址导航名站网址导航小编为爱好网页hTML5的朋友提供关于网页hTML5的相关知识。
- 本篇文章的具体介绍如下
复制具体代码 具体代码如下:
<input type="file" name="multipleFileUpload" multiple />
下面是页面的详细具体代码:
复制具体代码 具体代码如下:
<!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="重设">
</form>
</body>
</html>
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<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();
}
}
下面是具体效果图:
关于网页hTML5的相关知识就说到这里希望可以帮助朋友们。,html5,多相关站点文件上传网页hTML5教程 video 视频网页文章标签使用介绍