如何在jsp页面实现文件的上传和下载 ? 小弟没头绪,请各位高手指点迷津,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 上传有很多类库可以用的,比如jspUpload等等,很多的,google一下吧_________________技术圈:IT人的家 我建议用jspuploadsmart,下载这个组件后只需要写几行代码就可以了,非常简单 我上太难写了上传的代码 你参考下 给你部分有用的 这个是JSP页面 <tr> <th align="center" bgcolor="#ffffff" scope="row"> <div id="cz"> <input type="button" name="czbutt" class="input3" value="承装类上传资料" onClick="czsq()" disabled></div> </th> </tr>function czsq() { var url='czsq.jsp'; window.open(url,'newwindow','height=600,width=925,top=0,left=0,toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no'); }。。这里是指向的上传页面<%@ page language="java" contentType="text/html; charset=gbk" import="com.jspsmart.upload.*" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>企业技术及管理人员导入页面</title><style type="text/css">body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; background-color: #F0F9FF;}.style2 {color: #15427F}td { font-size: 12px; line-height: 150%; color: #333333; text-decoration: none;}.style3 {color: #FF0000}a { font-size: 12px; line-height: 150%; color: #052B77; text-decoration: none;}.style4 {color: #999999}.style6 {font-size: 13px}.a4 { font-size: 12px; line-height: 120%; color: #555555; text-decoration: none;}.a6 { font-size: 12px; line-height: 120%; color: #0000FF; text-decoration: none;}.input2 { BACKGROUND: #ffffff; BORDER-BOTTOM: #278CD8 1px solid; BORDER-LEFT: #278CD8 1px solid; BORDER-RIGHT: #278CD8 1px solid; BORDER-TOP: #278CD8 1px solid; COLOR: #1667BC; FONT-FAMILY: "arial"; FONT-SIZE: 12px; height: 16px; width: 140px}.input3 { BACKGROUND: #ffffff; BORDER-BOTTOM: #B3CCE8 1px solid; BORDER-LEFT: #B3CCE8 1px solid; BORDER-RIGHT: #B3CCE8 1px solid; BORDER-TOP: #B3CCE8 1px solid; COLOR: #1667BC; FONT-FAMILY: "arial"; FONT-SIZE: 12px; height: 16px; width: 140px}.input4 { BACKGROUND: #2171C5; BORDER-BOTTOM: #f4f4f4 1px solid; BORDER-LEFT: #f4f4f4 1px solid; BORDER-RIGHT: #f4f4f4 1px solid; BORDER-TOP: #f4f4f4 1px solid; COLOR: #ffffff; FONT-FAMILY: "arial"; FONT-SIZE: 12px; height: 20px; width: 60px}</style><script type="text/javascript">function reSize(){ window.resizeTo(500,300); window.moveTo(300,300); }</script></head><body onload="reSize()"><FORM name="czfrm" METHOD="POST" ACTION="uploadCz.jsp" ENCTYPE="multipart/form-data"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td height="40" background="images/bg04e.gif" bgcolor="#BDDBEE"> <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td>上传附件 <input type="FILE" name="FILE1" size="30"> <input type="submit" name="Submit" value="上传" > </td> </tr> </table> </td> </tr></table> </FORM><form name="czfr2"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FAFAD2"> <tr> <td> 上传说明: </td> </tr> <tr> <td> <pre> 说明1:企业的承装等级上传附件以及资料。 说明2:如果上传类别几种,可以依次上传。 说明3:例如上传承装3级的资料,把所有的 资料打包放到一个文件夹中,文件夹名字取 名为:承装三级,依次类推! </pre> </td> </tr> </table></form><%if(session.getAttribute("uploadOk") !=null){ out.println((String)session.getAttribute("uploadOk")); session.setAttribute("uploadOk",null);}%></body></html>这里是实现页面<%@ page contentType="text/html; charset=gbk" language="java" import="com.jspsmart.upload.*,java.util.*" %><html><head><title>文件上传处理页面</title></head><body><form name="CzloadFrm" ><%String djbno =(String)session.getAttribute("djbno");System.out.println("电监办ID:"+djbno);String path="";try{ String separator = null; java.util.Properties p = new Properties(System.getProperties()); String osName = p.getProperty("os.name"); java.io.File upfile=new java.io.File(djbno+"\\cz"); //判断操作系统 if(osName.indexOf("Win")!=-1) { separator = "\\"; if(!(upfile.isDirectory())) { upfile.mkdirs(); } path = djbno+"//"+"cz"; } else if(osName.indexOf("AIX")!=-1) { separator = "/"; if(!(upfile.isDirectory())) { upfile.mkdirs(); } path=djbno+"//"+"cz"; } System.out.println("cyPath: "+upfile.getAbsolutePath()); session.setAttribute("FilePath",upfile.getAbsolutePath()); if(separator==null) { return; } //实例化上载bean SmartUpload su = new SmartUpload(); //上传初始化 su.initialize(pageContext);// 设置上载的最大值// su.setMaxFileSize(500 * 1024*1024); //上载文件 // mySmartUpload.setAllowedFilesList("doc,txt"); //上传限制扩展名 // su.setAllowedFilesList("doc"); // 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat, jsp,htm,html扩展名的文件和没有扩展名的文件。 // su.setDeniedFilesList("exe,bat,jsp,htm,html,,"); su.upload(); // 循环取得所有上载的文件,由于刚才的上传选择文件网页upload,htm只有一个文件所以只循环一次,//如果需要一次上传多个文件,则修改upload,htm,增加多个文件选择INPUT for (int i=0;i<su.getFiles().getCount();i++) {// 取得上载的文件 com.jspsmart.upload.File myFile = su.getFiles().getFile(i); // 若文件不存在则继续 if (myFile.isMissing()) continue; //保存文件 try{ myFile.saveAs(path+separator+myFile.getFileName()); System.out.print("path="+path+"\n"+"myfile.getFileName上传文件是:"+myFile.getFileName()); }catch(SmartUploadException esm){esm.printStackTrace();} } String uploadOk= "\n<Script language=\"javaScript\"> alert('上传成功!');</Script>"; session.setAttribute("uploadOk",uploadOk); response.sendRedirect("czsq.jsp");}catch(Exception err){ err.printStackTrace();}%></form></body></html>OK了,你整理下就没有问题了 记得给分哦 java菜鸟求教 一个问题,帮忙看看 一句SQL查询语句,在JAVA中报错,在sqlserver中得到正确结果。 有谁能指教下 华为招聘 为什么我这样从jar文件中读取文件不行? 一个动画问题,一个星期了都没人解决(在线等) 初学者,很简单的问题,下面这一句做何解释? 初级问题,java里输入是什么? 如何取模? InstallAnywhere 8 Enterprise 如何打包程序中的参数 一个随机数设计,可不可以实现?
上传有很多类库可以用的,比如jspUpload等等,很多的,google一下吧_________________
技术圈:IT人的家
这个是JSP页面 <tr>
<th align="center" bgcolor="#ffffff" scope="row">
<div id="cz">
<input type="button" name="czbutt" class="input3" value="承装类上传资料" onClick="czsq()" disabled></div>
</th>
</tr>function czsq()
{
var url='czsq.jsp';
window.open(url,'newwindow','height=600,width=925,top=0,left=0,toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no');
}。。
这里是指向的上传页面
<%@ page language="java" contentType="text/html; charset=gbk" import="com.jspsmart.upload.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>企业技术及管理人员导入页面</title>
<style type="text/css">
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
background-color: #F0F9FF;
}
.style2 {color: #15427F}
td {
font-size: 12px;
line-height: 150%;
color: #333333;
text-decoration: none;
}
.style3 {color: #FF0000}
a {
font-size: 12px;
line-height: 150%;
color: #052B77;
text-decoration: none;
}
.style4 {color: #999999}
.style6 {font-size: 13px}
.a4 { font-size: 12px;
line-height: 120%;
color: #555555;
text-decoration: none;
}
.a6 { font-size: 12px;
line-height: 120%;
color: #0000FF;
text-decoration: none;
}
.input2 {
BACKGROUND: #ffffff; BORDER-BOTTOM: #278CD8 1px solid; BORDER-LEFT: #278CD8 1px solid; BORDER-RIGHT: #278CD8 1px solid; BORDER-TOP: #278CD8 1px solid; COLOR: #1667BC; FONT-FAMILY: "arial"; FONT-SIZE: 12px
; height: 16px; width: 140px}.input3 {
BACKGROUND: #ffffff; BORDER-BOTTOM: #B3CCE8 1px solid; BORDER-LEFT: #B3CCE8 1px solid; BORDER-RIGHT: #B3CCE8 1px solid; BORDER-TOP: #B3CCE8 1px solid; COLOR: #1667BC; FONT-FAMILY: "arial"; FONT-SIZE: 12px
; height: 16px; width: 140px}
.input4 {
BACKGROUND: #2171C5; BORDER-BOTTOM: #f4f4f4 1px solid; BORDER-LEFT: #f4f4f4 1px solid; BORDER-RIGHT: #f4f4f4 1px solid; BORDER-TOP: #f4f4f4 1px solid; COLOR: #ffffff; FONT-FAMILY: "arial"; FONT-SIZE: 12px
; height: 20px; width: 60px}</style>
<script type="text/javascript">
function reSize()
{
window.resizeTo(500,300);
window.moveTo(300,300);
}
</script>
</head>
<body onload="reSize()">
<FORM name="czfrm" METHOD="POST" ACTION="uploadCz.jsp" ENCTYPE="multipart/form-data">
<table width="98%" border="0" align="center" cellpadding="0"
cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td height="40" background="images/bg04e.gif" bgcolor="#BDDBEE">
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>上传附件
<input type="FILE" name="FILE1" size="30">
<input type="submit" name="Submit" value="上传" >
</td>
</tr>
</table>
</td>
</tr>
</table>
</FORM>
<form name="czfr2">
<table width="98%" border="0" align="center" cellpadding="0"
cellspacing="1" bgcolor="#FAFAD2">
<tr>
<td>
上传说明:
</td>
</tr>
<tr>
<td>
<pre>
说明1:企业的承装等级上传附件以及资料。
说明2:如果上传类别几种,可以依次上传。
说明3:例如上传承装3级的资料,把所有的
资料打包放到一个文件夹中,文件夹名字取
名为:承装三级,依次类推!
</pre>
</td>
</tr> </table>
</form>
<%
if(session.getAttribute("uploadOk") !=null)
{
out.println((String)session.getAttribute("uploadOk"));
session.setAttribute("uploadOk",null);
}
%>
</body>
</html>这里是实现页面<%@ page contentType="text/html; charset=gbk" language="java"
import="com.jspsmart.upload.*,
java.util.*" %>
<html>
<head>
<title>文件上传处理页面</title>
</head>
<body>
<form name="CzloadFrm" >
<%
String djbno =(String)session.getAttribute("djbno");
System.out.println("电监办ID:"+djbno);
String path="";
try
{
String separator = null;
java.util.Properties p = new Properties(System.getProperties());
String osName = p.getProperty("os.name");
java.io.File upfile=new java.io.File(djbno+"\\cz");
//判断操作系统
if(osName.indexOf("Win")!=-1)
{
separator = "\\";
if(!(upfile.isDirectory()))
{
upfile.mkdirs();
}
path = djbno+"//"+"cz";
}
else if(osName.indexOf("AIX")!=-1)
{
separator = "/";
if(!(upfile.isDirectory()))
{
upfile.mkdirs();
}
path=djbno+"//"+"cz";
}
System.out.println("cyPath: "+upfile.getAbsolutePath());
session.setAttribute("FilePath",upfile.getAbsolutePath()); if(separator==null)
{
return;
}
//实例化上载bean
SmartUpload su = new SmartUpload();
//上传初始化
su.initialize(pageContext);
// 设置上载的最大值
// su.setMaxFileSize(500 * 1024*1024);
//上载文件
// mySmartUpload.setAllowedFilesList("doc,txt"); //上传限制扩展名
// su.setAllowedFilesList("doc");
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat, jsp,htm,html扩展名的文件和没有扩展名的文件。
// su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
su.upload();
// 循环取得所有上载的文件,由于刚才的上传选择文件网页upload,htm只有一个文件所以只循环一次,
//如果需要一次上传多个文件,则修改upload,htm,增加多个文件选择INPUT
for (int i=0;i<su.getFiles().getCount();i++)
{
// 取得上载的文件
com.jspsmart.upload.File myFile = su.getFiles().getFile(i);
// 若文件不存在则继续
if (myFile.isMissing()) continue;
//保存文件
try{
myFile.saveAs(path+separator+myFile.getFileName());
System.out.print("path="+path+"\n"+"myfile.getFileName上传文件是:"+myFile.getFileName());
}catch(SmartUploadException esm){esm.printStackTrace();}
}
String uploadOk= "\n<Script language=\"javaScript\"> alert('上传成功!');</Script>";
session.setAttribute("uploadOk",uploadOk);
response.sendRedirect("czsq.jsp");
}
catch(Exception err)
{
err.printStackTrace();
}%>
</form>
</body>
</html>OK了,你整理下就没有问题了 记得给分哦