?? upload.java
字號:
package com.yuanchung.sales.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;
import org.apache.struts.upload.FormFile;
public class Upload {
static Logger logger = Logger.getLogger(Upload.class);
public static String upload(FormFile theFile, String filePath) {
logger.debug("開始上傳");
String fileName=null;
try {
// 開始上傳文件
fileName = new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"-"+theFile.getFileName();
InputStream stream = theFile.getInputStream(); // 把文件讀入
String upload = (filePath);
upload = upload.substring(0, upload.length() - 1).concat("\\");
if (!(new File(upload).isDirectory())) {
new File(upload).mkdirs();
}
OutputStream bos = new FileOutputStream(filePath + fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
stream.close();
}
catch (Exception e) {
logger.error(e);
}
return fileName;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -