上傳 存的是結對路徑D:\tomct.......
/**
* @Description: 生成上傳文件的文件名,文件名以時間+3位隨機數+"_"+文件的原始名稱
* @param filename
* 文件的原始名稱
* @return 時間+3位隨機數+"_"+文件的原始名稱
*/
private String makeFileName(String filename) { // 2.jpg
// 為防止文件覆蓋的現象發生,要為上傳文件產生一個唯一的文件名
// return UUID.randomUUID().toString() + "_" + filename;
// 日期格式化
DateFormat df = new SimpleDateFormat("yyyyMMddhhmmssSSS");
// 當前的時間
String name = df.format(new Date());
// 三位的隨機數
Random r = new Random();
// name=時間+三位的隨機數
for (int i = 0; i < 3; i++) {
name += r.nextInt(10); // +=會自動的將int轉換為string
}
return name + "_" + filename;
}
/**
* 為防止一個目錄下面出現太多文件,要使用hash算法打散存儲
* 使用"/" 支持linux系統
* @param filename
* 文件名,要根據文件名生成存儲目錄
* @param savePath
* 文件存儲路徑
* @return 新的存儲目錄
*/
private String makePath(String filename,String savePath){
//得到文件名的hashCode的值,得到的就是filename這個字符串對象在內存中的地址
int hashcode = filename.hashCode();
int dir1 = hashcode & 0xf; //0--15
int dir2 = (hashcode & 0xf0)>>4; //0-15
//構造新的保存目錄
String dir = savePath + "/" + dir1 + "/" + dir2+"/"; //upload\2\3 upload\3\5
//File既可以代表文件也可以代表目錄
File file = new File(dir);
//如果目錄不存在
if(!file.exists()){
//創建目錄
file.mkdirs();
}
return dir;
String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
File root = new File(savePath);
if (!root.exists()) {
//創建臨時目錄
root.mkdir();
}
InputStream is = new FileInputStream(file);
//得到上傳文件的擴展名 .xle
String fileExtName = fileFileName.substring(fileFileName.lastIndexOf(".")+1);
//得到文件的真實的保存目錄
String realSavePath = makePath(fileFileName, savePath);
//修改文件的存儲名字,防止文件多了出現重名保存失敗
String storeName = makeFileName(fileFileName);
//創建一個文件輸出流
OutputStream os = new FileOutputStream(realSavePath + "/" + storeName);
byte[] buffer = new byte[1024];
int length = 0;
while(-1 != (length = is.read(buffer, 0, buffer.length)))
{
os.write(buffer);
}
os.close();
is.close();
//數據庫存儲的字段和名字
String urlAndName = realSavePath +storeName ;
notice.setAttachment(urlAndName);
}catch (Exception e) {
e.printStackTrace();
return SUCCESS ;
}
下載 文件
public String download() throws ServletException, Exception {
HttpServletRequest req = ServletActionContext.getRequest();
Long id = Long.parseLong(req.getParameter("id"));
noticeQuery.setId(id);
System.out.println();
// 獲取文件的全名和絕對磁盤地址
Notice notice = noticeService.selectByPrimaryKey(noticeQuery.getId());
// String urlAndName = kbm.getKbmAdjunct();
String urlAndName = notice.getAttachment();
// 處理獲取到的上傳文件的文件名的路徑部分,只保留文件名部分
String url = urlAndName.substring(0, urlAndName.lastIndexOf("/"));
String storeName = urlAndName.substring( urlAndName.lastIndexOf("/")+1);
String realName = storeName.substring(storeName.indexOf("_")+1);
// 上傳的文件都是保存在/WEB-INF/upload目錄下的子目錄當中
File file = new File(url + "/" + storeName);
// 如果文件不存在
if (!file.exists()) {
// 錯誤提示
this.getServletResponse().setCharacterEncoding("utf-8");
this.getServletResponse().getWriter().write("您要下載的資源已被刪除!!");
// servletRequest.setAttribute("message", "您要下載的資源已被刪除!!");
// servletRequest.getRequestDispatcher("/message.jsp").forward(
// servletRequest, servletResponse);
return "";
}
// 處理文件名
// String realname = fileStoreName.substring(fileStoreName.indexOf("_")
// + 1);
// 設置響應頭,控制瀏覽器下載該文件
servletResponse.setHeader("content-disposition", "attachment;filename="
+ URLEncoder.encode(realName, "UTF-8"));
// 讀取要下載的文件,保存到文件輸入流
FileInputStream in = new FileInputStream(file);
// 創建輸出流
OutputStream out = servletResponse.getOutputStream();
byte buffer[] = new byte[1024];
int len = 0;
// 循環將輸入流中的內容讀取到緩沖區當中
while ((len = in.read(buffer)) > 0) {
// 輸出緩沖區的內容到瀏覽器,實現文件下載
out.write(buffer, 0, len);
}
// 關閉文件輸入流
in.close();
// 關閉輸出流
out.close();
return NONE;
}