`
weigang.gao
  • 浏览: 466400 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

jspsmart 文件上传

 
阅读更多

工程的目录结构以及所需要的jar包:

1.index.jsp页面如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>upload</title>
<script type="text/javascript">
function reload_filepath(path)
{
	document.certForm.de_file_path.value = path;
}
</script>
</head>
<body>
<h1>上传证件</h1>
 <form action="" name="certForm" method="post">
  <table border="1" cellpadding="0" cellspacing="0">
     <tr>
	    <td>
	           保存路径:
	    </td>
	    <td>
			<input name="de_file_path" size="50" maxlength="200" readonly="true" />
	    </td>
	    <td>
	      <a href="#" onClick='JavaScript:window.open("upload.jsp?type=cert_file&agid=00393","","top=200,left=300,width=300,height=130")'>[上传证书]</a>&nbsp;
	    </td>
    </tr> 
   </table>
 </form>
</body>
</html>

 

2.upload.jsp页面如下:

<%@ page contentType="text/html;charset=GBK"%>
<html>
<head>
<title>文件上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
获取参数:<%=request.getQueryString() %>
<form action="upload_post.jsp?<%=request.getQueryString()%>" method="post" enctype="multipart/form-data" name="form1">
  <input name="file_data" type="file" id="file_data">
  <input type="submit" name="Submit" value="上传">
</form>
<center><input type=button value="关闭" onclick="javascript:window.close()"></center>
</body>
</html>

 

3.upload_post.jsp页面如下:

<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="com.hsp.base.Configuration" %>
<%@page import="com.hsp.base.Constants"%>
<%@page import="com.hsp.util.LoggerUtils"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /><!--jsp:useBean标签用于在JSP页面创建bean实例,相当于new-->
<%
//--- 上传模块 ---
String Msg = "";
long MaxFileSize = 1000000;  
//System.out.println("start-------------------------------------");
try
{
	String field = request.getParameter("type");
	String filename = "";
	String new_filename = "";
	mySmartUpload.initialize(pageContext);
	mySmartUpload.upload();

	com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
	
    LoggerUtils.recordInfo(Constants.SMART_WEB_LOG, myFile.getFileName());
	
	if (myFile.getSize() < MaxFileSize)
	{
		if (!myFile.isMissing())
		{
			String TimeStamp = String.valueOf((new Date()).getTime());
			String path = "";
			if (field.equalsIgnoreCase("cert_file")) {
				path = Configuration.getFile_upload_path();//获取文件上传路径
				
				LoggerUtils.recordInfo(Constants.SMART_WEB_LOG, path);
				
				TimeStamp = request.getParameter("agid") + "_" + TimeStamp;
				LoggerUtils.recordInfo(Constants.SMART_WEB_LOG, TimeStamp);
			}
			filename = myFile.getFileName();
			//上传文件重命名
		    String[] arr_filename = filename.split("\\.");//需要转义一下,这里不能使用split(".")
		    LoggerUtils.recordInfo(Constants.SMART_WEB_LOG, "数组长度:"+arr_filename.length);
		    LoggerUtils.recordInfo(Constants.SMART_WEB_LOG, "文件名拆分:"+ arr_filename[0]+"---"+arr_filename[1]);
		    
		    String temp_filename = arr_filename[0]+"_"+TimeStamp;
		    LoggerUtils.recordInfo(Constants.SMART_WEB_LOG, "文件名temp_filename:"+ temp_filename);
		    
		    new_filename = temp_filename + "." + arr_filename[1];
		    LoggerUtils.recordInfo(Constants.SMART_WEB_LOG, "文件名new_filename:"+ new_filename);
		    
			File file = new File(path, new_filename);
			myFile.saveAs(file.getAbsolutePath());
			Msg = "上传成功!";		}
		else {
			Msg = "上传不成功![<a href=javascript:history.back()>返回</a>]";
		}
	}
	else
	{
		Msg = "体积过大![<a href=javascript:history.back()>返回</a>]";
	}
	if (field.equalsIgnoreCase("cert_file") && Msg.equalsIgnoreCase("上传成功!")) {
%>
	<Script language="JavaScript">
	    //window.opener指的是调用window.open()函数的窗口,这个页面需要有reload_filepath函数
		window.opener.reload_filepath("<%=new_filename%>");
		window.close();
	</Script>
<%
	}		
}
catch(Exception e)
{
	LoggerUtils.recordInfo(Constants.SMART_SYS_LOG, "gaowegiang"+e.getMessage());
	Msg = "上传失败:" +e.getMessage();
	
}
%>
<body>
<br>
<center>
<%out.print("<font size=2>" + Msg + "</font>");%><BR><BR>
<input type=button value="关闭" onclick="javascript:window.close()">
</center>
</body>

 

 

  • 大小: 29.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics