1.文件上傳操作,解決上傳中文名的問題。
<%
// 新建一個SmartUpload對象
SmartUpload su = new SmartUpload();
// 上傳初始化
su.initialize(pageContext);
// 設定上傳限制
// 1.限制每個上傳文件的最大長度。
su.setMaxFileSize(2000000000);
// 2.限制總上傳數據的長度。
su.setTotalMaxFileSize(2000000000);
// 3.設定允許上傳的文件(通過擴展名限制),僅允許doc,xls文件。
su.setAllowedFilesList("doc,xls,txt,exe,rar");
// 4.設定禁止上傳的文件(通過擴展名限制),禁止上傳帶有exe,bat,
su.setDeniedFilesList("bat,jsp,htm,html");
SimpleDateFormat sdf1 = null;
SimpleDateFormat sdf2 = null;
String upfileName = null;
String upfileContent =null;
String [] str = new String[10];
String fileName = null;
// 上傳文件
su.upload();
// su.getFiles()獲得上傳的文件數
for (int i = 0; i < su.getFiles().getCount(); i++) {
// 拿到每個文件對象
// 獲得兩個時間對象
// sdf1 年月日
// sdf2 時分秒毫秒
Date date = new Date();
sdf1 = new SimpleDateFormat("yyyyMMdd");
sdf2 = new SimpleDateFormat("HHmmssSSSS");
upfileName = sdf1.format(date);
upfileContent = sdf2.format(date);
java.io.File upfile = new File("E://"+upfileName);
com.jspsmart.upload.File file = su.getFiles().getFile(i);
fileName = file.getFileName();
str[i]=fileName;
// 判斷該文件是否存在如不存在繼續下次循環。
if (file.isMissing()) {
continue;
}
// 查看文件夾是否存在如何不存在將創建該文件夾
if (upfile.exists() == false) {
upfile.mkdir();
}
//文件轉碼
fileName = new String(fileName.getBytes("gbk"),"UTF-8");
// 文件保存
file.saveAs("E://"+upfileName+"/"+upfileContent+fileName);
Thread.sleep(100);
}
%>
2. 文件下載操作,解決中文問題。
<%@ page contentType="text/html;charset=UTF-8"
import="com.jspsmart.upload.*" %><%
// 新建一個SmartUpload對象
SmartUpload su = new SmartUpload();
// 初始化
su.initialize(pageContext);
// 設定contentDisposition為null以禁止瀏覽器自動打開文件,
//保證點擊鏈接后是下載文件。若不設定,則下載的文件擴展名為
//doc時,瀏覽器將自動用word打開它。擴展名為pdf時,
//瀏覽器將用acrobat打開。
su.setContentDisposition(null);
// 下載文件
//su.downloadFile("F://test.xls");
String url = "F://sk.xls";
url = new String(url.getBytes("ISO-8859-1"),"UTF-8");
// 下載文件 改名
su.downloadFile(url,null,java.net.URLEncoder.encode("大家好.xls","UTF-8"));
%>