当前位置: 代码迷 >> Web前端 >> java servlet common-fileupload 兑现的文件批量上传
  详细解决方案

java servlet common-fileupload 兑现的文件批量上传

热度:849   发布时间:2013-07-20 11:07:48.0
java servlet common-fileupload 实现的文件批量上传

结合前辈们的代码, 写了个用servlet?和 common-fileupload 组件实现的文件批量上传。

只是一个简单的框架,没有设置文件大小,类型等的限制,你可根据自己需要添加代码。

环境:tomcat 5

jdk 6 其实这些都关系不大了,只要你用的是现在主流的版本。

?

java代码:

servlet中 dopost方法部分

response.setContentType("text/html");
??response.setCharacterEncoding("UTF-8");
??String storePath=getServletContext().getRealPath("/uploadimg");
??PrintWriter out=response.getWriter();
??File uploadDir = new File(storePath);//得到该目录的文件对象
??if(!uploadDir.exists()){//判断该目录是否存在
??????????? if(!uploadDir.mkdir()){//如果不存在就建立该目录
??????????????? out.print("无法创建该目录!");//如果建立失败,给出提示
??????????????? return;
??????????? }
??????? }
??if(!ServletFileUpload.isMultipartContent(request)){//用 ServletFileUpload 类的静态方法 isMultipartContent 判断 request 是否是 multipart/form-data 类型
???????????? out.print("只能 multipart/form-data 类型数据");
???????????? return;
???????? }
??//创建 DiskFileItemFactory 对象
???? DiskFileItemFactory factory = new DiskFileItemFactory();
???? ServletFileUpload upload = new ServletFileUpload(factory);//创建 ServletFileUpload 对象,构造的时候传一个 DiskFileItemFactory 对象进去
???? upload.setHeaderEncoding("UTF-8");//设置普通字段名称和文件字段的文件名所采用的字符集编码
???? List list = null;
???? try {
??????????? list = upload.parseRequest(request);//解析 request 对象 得到一个包含 FileItem 对象的 list
??????????? System.out.println("~~~~~~~~~~~~~~~~~~~~~~~"+list.size());
??????? } catch (FileUploadException e) {
??????????? out.print("解析文件时出现问题:");
??????????? e.printStackTrace();
??????????? return;
??????? }
??????? Iterator it = list.iterator();//遍历list
??????? //防止批量上传图片 存储的名字相同
??????? int i=0;
??????? while(it.hasNext()){
??????????? FileItem fi = (FileItem)it.next();//类型转换
??????????? if(fi.isFormField()){//判断该 FileItem 对象是否是一个普通表单类型
??????????????? String name = fi.getFieldName();//得到普通表单类型的表单名
??????????????? String content = fi.getString("UTF-8");//用指定编码得到普通表单的值
??????????????? out.println(name+"->>:"+content+"<br>");
?????????????? // request.setAttribute(name, content);//把键值放入 request 对象
??????????? }else{
??????????? ? try{
???????????????????? String pathStr = fi.getName();//得到文件表单的值,就是用户本地的文件路径
???????????????????? //如果文件表单为空,则不处理
???????????????????? if(pathStr.trim().equals("")){
???????????????????????? continue;
???????????????????? }
???????????????????? int start = pathStr.lastIndexOf("//");//得到文件名在路径中的位置
???????????????????? int end = pathStr.lastIndexOf(".");
???????????????????? String fileName = pathStr.substring(start + 1,end);//得到文件名
???????????????????? String extention = pathStr.substring(end);//获得后缀名包括“.”
???????????????????? String curTime=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
???????????????????? String storeName=curTime+i+extention;//给出图片存放名字
???????????????????? File pathDest = new File( storePath , storeName );//创建上传上来的文件对象? 由服务器上存放文件的路径 + 文件名 组成
???????????????????? fi.write(pathDest);//写文件
???????????????????? String name = fi.getFieldName();//得到文件表单的名称
???????????????????? request.setAttribute(name, fileName);//把表单名、文件名放入 request
???????????????????? out.println(fileName+"<br>");
???????????????????? out.print(storePath+"<br>");
???????????????????? out.println("storeName->>"+storeName+"<br>");
???????????????????? i++;
???????????????? }catch(Exception e){
???????????????????? out.print("存储文件错误:");
???????????????????? e.printStackTrace();
???????????????????? return;
???????????????? }
??????????? }
??????? }

?

页面代码,用的是dom技术

<%@ page language="java" contentType="text/html; charset=UTF-8"
??? pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="content-type" content="text/html">
<title>locale photo</title>
<mce:script type="text/javascript"><!--
function AddMoreRow()
{

var oRow=event.srcElement.parentNode.parentNode;
var oTable=oRow.parentNode.parentNode;
var rowCount=oTable.rows.length;
oNewRow=oTable.insertRow(rowCount);
oNewRow.insertCell(0).innerHTML="名称*";
oNewRow.insertCell(1).innerHTML="<input type='text' name='Name' size='15 maxlength='50'>";
oNewRow.insertCell(2).innerHTML="文件路径*";
oNewRow.insertCell(3).innerHTML="<input type='file' name='FilePath' size='15' onKeyDown='DisabledKeyInput()' />";
oNewRow.insertCell(4).innerHTML="说明*";
oNewRow.insertCell(5).innerHTML="<input type='text' name='Description' size='45'>";
oRow.all("DelBtn").disabled=false;
oNewRow.all("DelBtn").disabled=false;
}
function DeleteMoreRow()
{
var oRow=event.srcElement.parentNode.parentNode;
var oTable=oRow.parentNode.parentNode;
var rowCount=oTable.rows.length;
if(oTable.rows.length>1)
{
oTable.deleteRow(rowCount-1);
if(oTable.rows.length==1)
{
oTable.all("DelBtn").disabled=true;
}
}
}
function DisabledKeyInput()
{
if(event.keyCode!=8&&event.keyCode!=46)
{
event.returnValue=false;
}
}
// --></mce:script>
</head>

<body topmargin="10">
<form action="../ClothesAddServlet" method="post" enctype ="multipart/form-data">
批量上传
<table id="t1">

<tr>

<td >名称*</td>
<td><input type="text" name="name" size="15" maxlength="50"></td>
<td >文件路径*</td>
<td><input type="file" name="FilePath" size="15" onKeyDown="DisabledKeyInput()" /></td>

<td >说明*</td>
<td><input type="text" name="Description" size="45"><input type="button" value="增加" onclick="AddMoreRow()"></td>

<td valign="bottom">&nbsp;
<input type="button" id="DelBtn" value="删除" disabled="disabled" onClick="DeleteMoreRow()">
</td>

</tr>

</table>
<table>
<tr align="center">
<td align="center">
<input type="submit" value="保存">&nbsp;&nbsp;<input type="reset" value="取消"></td>
</tr>
</table>
</form>
</body>
</html>

?

  相关解决方案