文件上传问题-----急,在线等

jruddy 2004-09-13 08:49:19
怎么能批量上传文件呢,一次性的选择多个 ,就像realOne 的添加文件,


html有标签吗 就像 file

jsp,java 实现也成

先谢了
...全文
211 点赞 收藏 16
写回复
16 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
jruddy 2004-09-30
虽然和我想要不太一样,但还是谢谢大家
回复
weicairong 2004-09-14
bucuo
回复
lecy 2004-09-14
up
回复
28569050 2004-09-14
pic2.jsp
<%@ page contentType="text/html; charset=GBK"%>
<%@ page language="java"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<link rel="stylesheet" type="text/css" href="../style/style.css">
<title>察看图片</title>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#BEDEFF" bgcolor="#F2F8FF" width="100%" height="100%">
<tr>
<td width="100%" align="center"><br>
请将图片直接拖到编辑区
<%String img0,img1,img2,img3,img4,img5,img6,img7,img8,img9,img10;

if(session.getValue("img0")!=null)
{ img0=(String)session.getValue("img0");
out.println("<table width='100%'>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("<img src='"+img0+"' align='left' alt='图片1'></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("图片1</td>");
out.println("</tr>");
out.println("</table>");
}

if(session.getValue("img1")!=null)
{ img1=(String)session.getValue("img1");
out.println("<table width='100%'>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("<img src='"+img1+"' align='left' alt='图片1'></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("图片2</td>");
out.println("</tr>");
out.println("</table>");
}
if(session.getValue("img2")!=null)
{ img2=(String)session.getValue("img2");
out.println("<table width='100%'>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("<img src='"+img2+"' align='left' alt='图片2'></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("图片3</td>");
out.println("</tr>");
out.println("</table>");
}
if(session.getValue("img3")!=null)
{ img3=(String)session.getValue("img3");
out.println("<table width='100%'>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("<img src='"+img3+"' align='left' alt='图片3'></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("图片4</td>");
out.println("</tr>");
out.println("</table>");
}
if(session.getValue("img4")!=null)
{ img4=(String)session.getValue("img4");
out.println("<table width='100%'>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("<img src='"+img4+"' align='left' alt='图片4'></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("图片5</td>");
out.println("</tr>");
out.println("</table>");
}
if(session.getValue("img5")!=null)
{ img5=(String)session.getValue("img5");
out.println("<table width='100%'>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("<img src='"+img5+"' align='left' alt='图片5'></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("图片6</td>");
out.println("</tr>");
out.println("</table>");
}
if(session.getValue("img6")!=null)
{ img6=(String)session.getValue("img6");
out.println("<table width='100%'>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("<img src='"+img6+"' align='left' alt='图片6'></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("图片7</td>");
out.println("</tr>");
out.println("</table>");
}
if(session.getValue("img7")!=null)
{ img7=(String)session.getValue("img7");
out.println("<table width='100%'>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("<img src='"+img7+"' align='left' alt='图片7'></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("图片8</td>");
out.println("</tr>");
out.println("</table>");
}
if(session.getValue("img8")!=null)
{ img8=(String)session.getValue("img8");
out.println("<table width='100%'>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("<img src='"+img8+"' align='left' alt='图片8'></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("图片9</td>");
out.println("</tr>");
out.println("</table>");
}
if(session.getValue("img9")!=null)
{ img9=(String)session.getValue("img9");
out.println("<table width='100%'>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("<img src='"+img9+"' align='left' alt='图片9'></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("图片10</td>");
out.println("</tr>");
out.println("</table>");
}
if(session.getValue("img10")!=null)
{ img10=(String)session.getValue("img10");
out.println("<table width='100%'>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("<img src='"+img10+"' align='left' alt='图片10'></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='100%' align='center'>");
out.println("图片10</td>");
out.println("</tr>");
out.println("</table>");
}
for(int i=0;i<11;i++){
String img="img"+i;
session.removeValue(img);
}
%>
</td>
</tr>
</table>
</body>
</html>
回复
28569050 2004-09-14
upload.jsp

<%@ page contentType="text/html; charset=gb2312"%>
<%@ page language="java" import="popten.web.*" %>
<%@ page errorPage="er.jsp" %>
<%//@include file="cookieuser.jsp"%>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../style/style.css">
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Upload</title>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#BEDEFF" bgcolor="#F2F8FF" width="205" height="100%">
<tr>
<td width="100%" align="center">
<p ><form name="form1" enctype="multipart/form-data" method="post" action="uploadok.jsp">
<p>  1:
<input type="file" name="file1" size="10"> <br>
 2:
<input type="file" name="file6" size="10"> <br>
 3:
<input type="file" name="file2" size="10"> <br>
 4:
<input type="file" name="file7" size="10"><br>
 5:
<input type="file" name="file3" size="10"><br>
 6:
<input type="file" name="file8" size="10"> <br>
 7:
<input type="file" name="file4" size="10"> <br>
 8:
<input type="file" name="file9" size="10"> <br>
 9:
<input type="file" name="file5" size="10"><br>
10:
<input type="file" name="file10" size="10">
</p>
<p>
<input border="0" src="../img/button-upload.gif" name="Submit" type="image" class="button">
</p>
</form>
</td>
</tr>
</table>
</body>
</html>


uoloadok.jsp
<%@ page contentType="text/html;charset=GBK" %>
<%@ page language="java" import="popten.web.*,com.jspsmart.upload.*,java.sql.*"%>
<%@ page import="java.sql.*"%>
<%//@ page errorPage="er.jsp" %>
<%//@ include file="chelogin3.jsp"%>
<%@ page import="java.util.Date"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
boolean foundErr = false;
String errMsg="";
//String news_id = (String)session.getValue("news_id");
try{
mySmartUpload.initialize(pageContext);
mySmartUpload.setTotalMaxFileSize(1500000);
// mySmartUpload.setAllowedFilesList("gif,jpg,jpeg");
// mySmartUpload.setDeniedFilesList("exe,bat,jsp,php,doc,txt,asp");
mySmartUpload.upload();
Date nowTime=new Date();//声明时间变量
// String path = request.getRealPath("../upload/");
for(int i=0;i<mySmartUpload.getFiles().getCount();i++){
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
//i++;
if(!myFile.isMissing()){
if (myFile.getSize()<150000) {
if (myFile.getFileExt().toLowerCase().equals("jpg") || myFile.getFileExt().toLowerCase().equals("jpeg") || myFile.getFileExt().toLowerCase().equals("gif")){
String path = request.getRealPath("mdaimage/");
myFile.saveAs("mdaimage/up"+String.valueOf(nowTime.getTime())+i+"."+myFile.getFileExt().toLowerCase());//路径+up+时间+变量i+扩展名
String img="img"+i;//给session值
String pic="mdaimage/up"+String.valueOf(nowTime.getTime())+i+"."+myFile.getFileExt().toLowerCase();
session.putValue(img,pic);

//if(i==1){
//News.uploadPic(pic,news_id);
//}
}

else{
errMsg = errMsg + "□ 只能上传gif,jpg,jpeg图片!<br>";
foundErr = true;
}
}
else{
errMsg = errMsg + "□ 上传的图片不能超过100K!<br>";
foundErr = true;
}
}
}
}
catch (Exception e){
out.println("Unable to upload the file.<br>");
out.println("Error : " + e.toString());
}
if(foundErr) throw new Exception(errMsg);
response.sendRedirect("pic2.jsp");
%>
回复
fashchina 2004-09-14
你下载个jspsmart
里面例子都有的很全的
回复
jiaxiong_chen 2004-09-14
你没看到吗??他做循环了
回复
jruddy 2004-09-14
怎么没人呀
回复
phptong 2004-09-13
楼上兄弟先谢了,写的这么全
回复
caiyi0903 2004-09-13
<%@ page import="com.jspsmart.upload.SmartUpload"%>
<%
com.jspsmart.upload.SmartUpload mySmartUpload=new com.jspsmart.upload.SmartUpload();
//初始化
mySmartUpload.initialize(pageContext);
//设置上载的最大值
mySmartUpload.setMaxFileSize(500 * 1024*1024);
//上载文件
mySmartUpload.upload();
//循环取得所有上载的文件
for (int i=0;i<mySmartUpload.getFiles().getCount();i++){
//取得上载的文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing()) {
//取得上载的文件的文件名
String myFileName=myFile.getFileName();
//取得不带后缀的文件名
String suffix=myFileName.substring(0,myFileName.lastIndexOf('.'));
//取得后缀名
String ext= mySmartUpload.getFiles().getFile(0).getFileExt();
//取得文件的大小
int fileSize=myFile.getSize();
//保存路径
String aa = getServletContext().getRealPath("/")+"upload/";
String trace=aa+myFileName;
//取得别的参数
//String explain=(String)mySmartUpload.getRequest().getParameter("text");
//String send=(String)mySmartUpload.getRequest().getParameter("send");

java.io.File file = new java.io.File(trace);
if(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if(file.exists()) {
out.println("该文件已经存在!<br>");
return;
}

//将文件保存在服务器端
out.println("正在上传文件,请稍后...<br>");
out.flush();
myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL);

out.println("文件上载成功!!!<br>正在处理,请稍后...<br>");
out.flush();

Upload upload = new Upload();
String tmpDir = aa+System.currentTimeMillis();
int result = upload.uploadMinority(trace,tmpDir+"/",aa+"pic/",12,"1");
if(result == 0)
out.println("文件处理完成.<br>");
else if(result == -1)
out.println("文件处理失败.压缩文件有问题,目录及文件名只能是英文或数字.<br>");
else if(result == -2)
out.println("文件处理失败.文件数或目录不对<br>");
else
out.println("文件处理失败.未知错误.<br>");

if(result != 0) {
upload.deleteFile(trace);
upload.deleteFile(tmpDir);
}
} else {
out.println("上载失败!!!");
return;
}//end if
}//end for
%>
回复
sxgz 2004-09-13
我是用struts+js做的,是不是像网易邮箱那种效果?
回复
huanghelang 2004-09-13
是啊
回复
lianeng 2004-09-13
用jspsmartupload上传
回复
用jspsmartupload上传啊,可以上传多个文件的
回复
AgathaZ 2004-09-13
我也想知道。
回复
jruddy 2004-09-13
caiyi0903(willpower) ,写的不错,先谢了,

可我就是想知道,他怎么能 获取上传得文件, 用多个 file 标签吗?

我想用一个 类似file 的 东东,选取多个文件,完成 多个文件上传。


大家多帮忙呀, caiyi0903(willpower) 结贴在谢你

回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2004-09-13 08:49
社区公告
暂无公告