?? uploadfile.java
字號:
package com.wy.tool;
import java.io.*;
import java.util.Date;
import org.apache.struts.upload.FormFile;
public class UploadFile {
public String upload(String dir, FormFile formFile) throws Exception {
Date date = new Date();
// 取欲上傳的文件的名字和長度
String fname = formFile.getFileName();
// 將上傳時間加入文件名
int i = fname.indexOf(".");
String name = String.valueOf(date.getTime());
String type = fname.substring(i + 1);
fname = name + "." + type;
InputStream streamIn = formFile.getInputStream(); // 創建讀取用戶上傳文件的對象
File uploadFile = new File(dir); // 創建把上傳數據寫到目標文件的對象
if (!uploadFile.exists() || uploadFile == null) { // 判斷指定路徑是否存在,不存在則創建路徑
uploadFile.mkdirs();
}
String path = uploadFile.getPath() + "/" + fname;
OutputStream streamOut = new FileOutputStream(path);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
formFile.destroy();
return fname;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -