使用FileUpload上传文件


<%

%>
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ page import="com.qunar.dataservice.quartz.*,com.qunar.dataservice.common.*,com.qunar.dataservice.util.*,java.util.*,java.io.File,com.qunar.dataservice.extract.ImageTool"%>
<%@ page import="org.apache.commons.fileupload.*,org.apache.commons.fileupload.disk.*"%>
<html>
<head>
<title>保存上传文件</title>
</head>
<%
String hotelseq = null;
FileItemFactory factory = new DiskFileItemFactory();
FileUpload fu = new FileUpload(factory);
// 设置允许用户上传文件大小,单位:字节
fu.setSizeMax(100000000);
// maximum size that will be stored in memory?
// 设置最多只允许在内存中存储的数据,单位:字节
//fu.setSizeThreshold(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
//fu.setRepositoryPath("C:\\TEMP");
//开始读取上传信息
List fileItems = fu.parseRequest(request);
%>
<body bgcolor="#FFFFFF”text="#000000” leftmargin="0”topmargin="40”marginwidth="0” marginheight="0”>
<font size="6”color="blue”>文件列表:</font>
<center>
<table cellpadding=0 cellspacing=1 border=1 width="100%">
<tr>
<td bgcolor="#008080">文件名</td>
<td bgcolor="#008080">URL</td>
</tr>
<%
// 依次处理每个上传的文件
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>> ();
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
//忽略其他不是文件域的所有表单信息

if(item.isFormField()){
if(item.getFieldName().equals("hotelseq")){
hotelseq = item.getString();
System.out.println(hotelseq);
}

}
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;

//保存上传的文件到指定的目录
int loc = name.lastIndexOf(".");
long curTime = System.currentTimeMillis();
name = "" + curTime + name.substring(loc);
File f = new File(DataServiceWebConfig.RootPath + "spider/upload" +name);
item.write(f);

}
}

%>
</table>

<br/><br/>
<a href="index.htm">返回上传页面</a>
</center>
</body>
</html>



<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
var i = 1;
function add(){
//var tb = document.getElementById("upt");
var tb = document.getElementById("tbody");
var tr = document.createElement("tr");
var td = document.createElement("td");
var inu = document.createElement("input");
//inu.type="file";
//inu.size="40";
inu.setAttribute("type","file");
inu.setAttribute("size","40");
i++;
//inu.name = "file"+i;
inu.setAttribute("name","file"+i);
//td.width="100%";
//td.colspan = "2";
td.setAttribute("width","100%");
td.setAttribute("colspan","2");
td.appendChild(inu);
tr.appendChild(td);
//tb.childNodes[0].appendChild(tr);
//tb.body.appendChild(tr);
//var tbody = document.getElementById("tbody");
tb.appendChild(tr);

}
</script>
</head>
<body bgcolor= "#FFFFFF" text= "#000000" leftmargin= "0" topmargin= "40" marginwidth= "0" marginheight= "0" >
<center>
<h1></h1>

<form name= "uploadform" method= "POST" action= "save.jsp" ENCTYPE= "multipart/form-data" >
<table id="upt" border= "1" width= "450" cellpadding= "4" cellspacing= "2" bordercolor= "#9BD7FF" >
<tbody id="tbody">
<tr><td width= "100%" colspan= "2" >
<input name= "file1" size= "40" type= "file" >
</td></tr>
<tbody>
</table>
<br/><br/>
<table>
<tr><td align= "center" ><input value= "add file" type= "button" onclick= "javascript:add()" /></td></tr>
</table>

<table><tr><td>hotelseq:<input name="hotelseq" type="text"/> </td></tr></table>
<table>
<tr><td align= "center" ><input name= "upload" type= "submit" value= "upload" /></td></tr>
</table>

</form>
</center>
</body>
</html>
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页