java檔案上傳(單檔案 多檔案)與刪除,java檔案上傳

來源:互聯網
上載者:User

java檔案上傳(單檔案 多檔案)與刪除,java檔案上傳

/**
* 檔案上傳--單檔案
*
* @param request
* @param response
* @param path
* 檔案存放路徑(path為WebApp\後面的內容)
* @return
*/
public final static String fileUpload(HttpServletRequest request,
HttpServletResponse response, String path) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
MultipartFile mFile = null;
for (Iterator<?> i = fileMap.keySet().iterator(); i.hasNext();) {
Object obj = i.next();
mFile = (MultipartFile) fileMap.get(obj);
}
String filePath = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
try {
// 得到上傳的檔案的檔案名稱
String filename = mFile.getOriginalFilename();
// 擷取檔案尾碼名
if (filename != null && !("").equals(filename)) {
String fileExt = filename.substring(filename.lastIndexOf("."));
// 按時間格式重建檔案名稱
String newFileName = sdf.format(new Date())
+ (int) (Math.random() * 100) + fileExt;
filePath = path + "/" + newFileName;
// 得到上傳伺服器的實體路徑
path = request.getSession().getServletContext()
.getRealPath("\\" + path);
// 檔案流寫到伺服器端
File saveFile = new File(path, newFileName);
FileCopyUtils.copy(mFile.getBytes(), saveFile);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return filePath;
}

/**
* 檔案上傳--多檔案
*
* @param request
* @param response
* @param filePaths
* (fileinputId,WebApp\後面的內容)
* @return
*/
public final static Map<String, Object> fileUploads(
HttpServletRequest request, HttpServletResponse response,
String path) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
Map<String, Object> filePaths = new HashMap<String, Object>();
// 得到上傳伺服器的實體路徑
String fileUrl = request.getSession().getServletContext()
.getRealPath("\\" + path);
for (Iterator<?> i = fileMap.keySet().iterator(); i.hasNext();) {
Object obj = i.next();
MultipartFile mFile = (MultipartFile) fileMap.get(obj);
// 得到上傳的檔案的檔案名稱
String filename = mFile.getOriginalFilename();
if (filename == "" || filename == null) {
continue;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
// 擷取檔案尾碼名
String fileExt = filename.substring(filename.lastIndexOf("."));
// 按時間格式重建檔案名稱
String newFileName = sdf.format(new Date())
+ (int) (Math.random() * 100) + fileExt;
String filePath = path + "/" + newFileName;
// 檔案流寫到伺服器端
try {
filePaths.put(obj.toString(), filePath);
File saveFile = new File(fileUrl, newFileName);
FileCopyUtils.copy(mFile.getBytes(), saveFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return filePaths;
}

/**
* 刪除檔案,
*
* @param request請求
* @param filePath檔案路徑
* (static/upload/...)
* @return
*/
public static boolean fileDelete(HttpServletRequest request, String filePath) {
String fileUrl = request.getSession().getServletContext()
.getRealPath("\\" + filePath);// 得到上傳伺服器的實體路徑
File file = new File(fileUrl);
fileDelete(file);
return false;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.