使用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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值