首先在MyEclipse項目中導入第三方smartupload.jar 包資源
------------------------------JSP頁面--------------------------------
<body>
<h1>文件上傳示例</h1> <br>
<form action="uploadManage.jsp" enctype="multipart/form-data" method="post">
上傳文件:<input type="file" name="f1"/><br/>
上傳文件:<input type="file" name="f2"/><br/>
上傳文件:<input type="file" name="f3"/>(文件類型可以是:*.jpg,*.png,*.doc) <br/>
<input type="submit" value="上傳"/>
</form>
</body>
-----------------------------表單提交處理頁面--------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.jspsmart.upload.*" %>
<%
//1.實例化上傳對象
SmartUpload su = new SmartUpload();
//2.初始化上傳 上下文
su.initialize(pageContext);
com.jspsmart.upload.File file = null;
try{
su.setCharset("UTF-8"); //設置上傳對象編碼
//設置允許上傳的文件類型
String yx="jpg,gif,png,rar";
su.setAllowedFilesList(yx);//允許上傳
String not ="jsp,php,html,js,exe,bat,dll";
su.setDeniedFilesList(not);//不允許上傳的內容
//3.執行上傳操作
su.upload();
//得到上傳文件總數
int count=su.getFiles().getCount();
//for循環
for(int i=0;i<count;i++){
//4.得到上傳的單個文件對象
file = su.getFiles().getFile(i);//get --------->i
if(!file.isMissing()){ //是否有該文件
file.setCharset("UTF-8"); //設置文件編碼
//5.確定上傳文件夾 上傳是將上傳到tomcat網站對應目錄
String loadFile="upload\\";
//6.將文件名 連接到上傳路徑後面
loadFile += file.getFileName();
//7.確定保存
file.saveAs(loadFile, SmartUpload.SAVE_VIRTUAL);
System.out.print(loadFile);//打印上傳後的路徑
}
}
}catch(Exception ex){
out.print(ex.getMessage());
}
%>