日期:2014-05-16  浏览次数:20320 次

jsp上传多个文件smartupload.jar
显示层代码:

?

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

?

?

?

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

?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

?

<html>

?

? <head> ? ?

?

? ? <title>My JSP 'index.jsp' starting page</title>

?

? ? <script type="text/javascript">

?

? ? function addFile(){

?

? ? var myTB = document.getElementByIdx_x_x("myTB");

?

? ? var rowNum = myTB.rows.length;

?

? ? var newRow = myTB.insertRow(rowNum);

?

? ? var cells_0 = newRow.insertCell(0);

?

? ? cells_0.innerHTML = "上传文件:";

?

? ? var cells_1 = newRow.insertCell(1);

?

? ? cells_1.innerHTML = "<input type='file' name='nfile' />";

?

? ?

?

? ? }

?

? ? </script>

?

? </head>

?

??

?

? <body>

?

? ?<form enctype="multipart/form-data" method="post" action="doUpload.jsp">

?

? ? <table id="myTB">

?

? ? <tr>

?

? ? <td>上传文件:</td>

?

? ? <td><input type="file" name="nfile" /></td>

?

? ? </tr>

?

? ?

?

? ? </table>

?

? ?<input type="button" value="添加" onclick="addFile()"><br>

?

? ?<input type="submit" value="上传">

?

? ?</form>

?

?

?

? </body>

?

</html>

?

?

?

============================================================================

?

逻辑层代码:

?

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

?

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

?

<%

?

SmartUpload su = new SmartUpload();

?

su.initialize(pageContext);

?

try{

?

su.setAllowedFilesList("jpg,jpeg,gif");

?

su.setDeniedFilesList("exe");

?

su.setMaxFileSize(1024*1024*1024*5);

?

su.setTotalMaxFileSize(1024*1024*1024*20);

?

su.setCharset("gbk");

?

su.upload();

?

}catch(Exception e){

?

out.print("您选择的文件非法或者长度超限,请检查!");

?

e.printStackTrace();

?

}

?

for(int i=0;i<su.getFiles().getCount();i++){

?

File file = su.getFiles().getFile(i);

?

if(!file.isMissing()){

?

Random rd = new Random();

?

Calendar cd = Calendar.getInstance();

?

String fileName = String.valueOf(cd.get(Calendar.YEAR))+String.valueOf(cd.get(Calendar.MONTH)+1)+String.valueOf(cd.get(Calendar.DATE))+

?

String.valueOf(cd.get(Calendar.MINUTE))+String.valueOf(cd.get(Calendar.SECOND))+String.valueOf(rd.nextInt(100))+"."+file.getFileExt();

?

String path = "upload\\";

?

path += fileName;

?

file.saveAs(path,SmartUpload.SAVE_VIRTUAL);

?

out.print("文件上传成功");

?

}

?

}

%>