jspsmart实现页面不跳转上传和下载文件(一)

来源:互联网 发布:iapp源码 编辑:程序博客网 时间:2024/06/05 08:45

    jspsmart很多人都用过,高手也是如云滴多,网上资料更是漫天飞,不过对于我这个新手菜鸟,将文件的上传下载坐下来,感触还是很深的,有很多东西需要在这里沉淀下来。为了自己,也为了像我一样的菜鸟。

    如果不想走后台action,直接使用jsp来完成文件的上传下载,jspsmart是最好的工具了。这里我就开始才从文件的上传开始讲起吧!
1、在网上下载一个jspsmart.jar,并放入工程的classpath下,这里就不细说了。

2、为了实现文件上传页面不跳转的功能,我这里在页面中添加了一个iframe,iframe中引入另外一个jsp,这个jsp中包含一个form,如下iframe的内容:

<iframe src="<%=path%>/service/communication/bulletinIssueAttach.jsp" name="attach" width="680" height="80" scrolling="no"frameborder="0" align="left" >                                                          </iframe>

在bulletinIssueAttach.jsp中存在一个form,form的内容很简单就是一个file类型的控件,内容如下:

<body><div class="right_middle_function"><div class="content_step1" style="border: none;margin: 0px;"><table border="0" align="left" cellpadding="0" cellspacing="0"><tr><td height="45" align="left" style="border: none;margin: 0px;border-bottom: none;"><form action="<%=path%>/service/communication/bulletinIssueUpload.jsp" method="post" enctype="multipart/form-data" name="form1" target="_self">                                <ul style="border: none; ">                                <li style="width:40px;">附件:</li>                                <li style="width:100px;"><input type="file" name="filename" width="1" size="25"></li><li style="width:100px;"><input type="submit" name="Submit" value="上传附件"></li></ul>                    </form></td></tr></table><div class="clearfloat"></div></div><div class="clearfloat"></div></div></body>

当点击“上传附件”的时候就会将表单提交至bulletinIssueUpload.jsp,而在bulletinIssueUpload.jsp中主要是jspsmart的处理文件上传的逻辑,如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@page import="java.io.*"%><%@page import="java.io.File"%><%@page import="com.jspsmart.upload.*"%><%@page import="java.util.*"%><%@page import="jxl.*"%><%String myFileName = "";String saveurl = "";SmartUpload mySmartUpload = new SmartUpload();long file_size_max = 1048576*10;String ext = "";String url = "\\upload\\";//初始化mySmartUpload.initialize(pageContext);//只允许上载此类文件try {mySmartUpload.setAllowedFilesList("txt,TXT,xls,XLS,xlsx,doc,docx");//此处的文件格式可以根据需要自己修改//上传文件 mySmartUpload.upload();} catch (Exception e) {%><SCRIPT language=javascript>alert("只允许上传txt,xls,xlsx,doc,docx类型文件!");history.back();</script><%}try {com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);if (myFile.isMissing()) {%><SCRIPT language=javascript>alert("请先选择要上传的文件");history.back();</script><%} else {myFileName = myFile.getFileName(); //取得上载的文件的文件名ext = myFile.getFileExt(); //取得后缀名int file_size = myFile.getSize(); //取得文件的大小if (file_size < file_size_max) {saveurl = application.getContextPath() + url;if (new File(saveurl).exists() == false) {new File(saveurl).mkdirs();// 如果不存在文件夹,新建一个}saveurl +=  myFileName; //保存路径myFile.saveAs(saveurl, SmartUpload.SAVE_PHYSICAL);saveurl=saveurl.replace("\\","\\\\");java.io.File outfile = new java.io.File(saveurl);FileReader fr = new FileReader(outfile);BufferedReader br = new BufferedReader(fr);String line = br.readLine();while (line != null) {try {Map<String, String> extenalAttrs = new HashMap<String, String>();// 这里的操作时防止从某部分开始的属性都为空,则造成数组长度不够,但符合要求line = line.trim() + "\\n";String[] cols = line.split("\\|");} catch (Exception e) {%>alert("附件上传失败,原因:<%=e.getMessage() %>");<%}line = br.readLine();}br.close();fr.close();%><SCRIPT language=javascript>alert("附件 <%=myFileName%> 上传成功!");parent.document.getElementById("attachRealPath").value='<%=saveurl%>';history.back();//parent.submitData('<%=saveurl%>');</script><%} else {%><SCRIPT language=javascript>alert("上传的文件太大,不能大于10MB");</script><%}}} catch (Exception e) {%>alert("附件上传失败,原因:<%=e.getMessage() %>");<%}%>

这样上传文件后,页面的跳转只在iframe中,主页面确没有跳转。到这里文件就上传成功了。
防止中文名乱码,界面上一定要设置为:contentType="text/html; charset=UTF-8"

jspsmart相关的资料,请参考一下这里面的API:http://www.blogjava.net/renyangok/articles/51362.html

原创粉丝点击