b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

jspsmart.jar_smartupload.jar下载_eclipse发布jar包jsp

电脑杂谈  发布时间:2017-01-20 17:11:55  来源:网络整理

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

如果不想走后台action,直接使用jsp来完成文件的上传下载,jspsmart是最好的工具了。jspsmart.jar这里我就开始才从文件的上传开始讲起吧!

1、在网上下载一个jspsmart.jar,并放入工程的classpath下,这里就不细说了。

jspsmart.jar_eclipse发布jar包jsp_smartupload.jar下载

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类型的控件,内容如下:

smartupload.jar下载_jspsmart.jar_eclipse发布jar包jsp

<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.SE_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中,主页面确没有跳转。jspsmart.jar到这里文件就上传成功了。

防止中文名乱码,界面上一定要设置为:contentType="text/html; charset=UTF-8"


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-27631-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...