您的位置:首页 > Web前端 > JavaScript

jspsmart实现文件的上传与下载

2010-06-12 21:32 513 查看
注意~~~~~~~~`:

《1》使用之前需要自己下载jspSmartUpload.jar包 这里找到一个支持中文的jar包,下载地址如下: http://www.blogjava.net/Files/hijackwust/jsmartcom_zh_CN.rar
把压缩包里面的jar拷贝到工程的lib下面即可

《2》在项目的webRoot下新建一个文件夹(uploadfile),文件夹里新建一个文件(test1.txt)

~~~~~~~~~~~~~~~~~~~~~~~~~~文件的上传~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>

<%@ page import="com.jspsmart.upload.*"%>

<jsp:useBean id="SU" scope="page" class="com.jspsmart.upload.SmartUpload" />

<html>

<head>

<title>上载附件 </title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

</head>

<body>

<center>正在上传文件...</center>

<%

//上载附件

try

{

SU.initialize(pageContext); //上传初始化。

SU.service(request,response);

SU.upload(); //上传文件。

SU.setAllowedFilesList("doc,txt"); // 设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。

SU.setDeniedFilesList("exe,bat,jsp,htm,html,,"); //设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat, jsp,htm,html扩展名的文件和没有扩展名的文件。

SU.setMaxFileSize(10000); // 限制每个上传文件的最大长度。

SU.setTotalMaxFileSize(20000); // 限制总上传数据的长度。

String fn=SU.getFiles().getFile(0).getFileName(); //获得上传名称

SU.save("uploadfile/");//文件保存的目录为UploadDir

out.println("<br>成功上传文件,请查看 uploadfile/"+fn+"文件<br>确认文件是否上传成功!");

}

catch(Exception ex)

{

ex.printStackTrace();

}

%>

<a href="UpLoad.html"><div>重新上传文件</div></a>

</body>

</html>

<html>

<head>

<title>文件上传</title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

</head>

<body>

<p align="center">上传文件选择</p>

<FORM method="POST" action="UpLoad.jsp" enctype="multipart/form-data">

<input type="hidden" name="TEST" value="good">

<table width="75%" border="1" align="center">

<tr>

<td><div align="center">1:

<input type="FILE" name="FILE1" size="30">

</div></td>

</tr>

<tr>

<td><div align="center">

<input type="submit" name="Submit" value="上传它!">

</div></td>

</tr>

</table>

</FORM>

</body>

</html>

~~~~~~~~~~~~~~~~~~~~~~~~文件的下载~~~~~~~~~~~~~~

<%@ page contentType="text/html;charset=gb2312" %>

<%@ page import="com.jspsmart.upload.*"%>

<jsp:useBean id="SU" scope="page" class="com.jspsmart.upload.SmartUpload" />

<html>

<head><title></title></head>

<body>

<%

// 初始化

SU.initialize(pageContext);

//SU.service(request,response);

// 设定contentDisposition为null以禁止浏览器自动打开文件,

//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为

//doc时,浏览器自动用Word打开它。扩展名为pdf时,浏览器用Acrobat打开。自动进行关联。

SU.setContentDisposition(null);

//下载文件

SU.downloadFile("/uploadfile/test1.txt");

out.clear();

out=pageContext.pushBody();

%>

</body>

</html>

<html>

<head>

<title>下载</title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

</head>

<body>

<a href="DownLoad.jsp"><h2 align="center">点击下载</h2></a>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: