?? testupload.java
字號:
package com.lfs;
import java.io.File;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
public class TestUpLoad {
private static int a = 0;
public String TestUpLoad(HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (a > 2) {
throw new Exception("同時導入的用戶太多,請稍候再導入!!!!");
}
a += 1;
System.out.println("進入導入方法!!!!!");
SmartUpload mySmartUpload = new SmartUpload();
ServletConfig config = (ServletConfig) request
.getAttribute("ServletConfig");
try {
// Initialization
mySmartUpload.initialize(config, request, response);
mySmartUpload.setTotalMaxFileSize(1000000 * 1024);
mySmartUpload.setMaxFileSize(4000 * 1024);
// Upload
mySmartUpload.upload();
// }
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(
0);
// 取得上載的文件的文件名
String myFileNamesc = myFile.getFileName();
// 取得不帶后綴的文件名
// String suffix = myFileName
// .substring(0, myFileName.lastIndexOf('.'));
// 取得后綴名
String ext = mySmartUpload.getFiles().getFile(0).getFileExt();
// 保存路徑
String path = config.getServletContext().getRealPath("/")
+ "/upload/";
System.err.println(path);
// String trace=path+myFileName;
String myFileName = System.currentTimeMillis() + "." + ext;
String trace = path + "" + myFileName;
System.err.println("路徑是: " + path);
// Save the file with the original name
// in a virtual path of the web server
if (myFileNamesc != null && !myFileNamesc.equals("")) {
myFile.saveAs(trace, SmartUpload.SAVE_PHYSICAL);
}
/**
* 刪除原來的文件;
*/
if (this.deleteFile("")) {
System.err.println("文件刪除成功");
}
} catch (Exception e) {
e.printStackTrace();
throw new Exception("錯誤發生在" + e.getMessage());
} finally {
a -= 1;
}
return "";
}
public static boolean deleteFile(String url) {// url格式:/sca/
boolean flag = false;
File filed = new File(url);
try {
if (!filed.exists()) {
flag = false;
} else {
filed.delete();
}
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -