Java tutorial
/******************************************************************************* * * Copyright (C) 2009 JST-BIRD MassBank * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ******************************************************************************* * * FileUpload NX * * ver 1.0.0 2009.02.02 * ******************************************************************************/ package massbank; import java.io.File; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.fileupload.DiskFileUpload; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; /** * FileUpload NX */ public class FileUpload extends DiskFileUpload { // ftHgAbv??[hpX public static final String UPLOAD_PATH = System.getProperty("java.io.tmpdir"); // ?o?pX private String outPath = UPLOAD_PATH; // NGXg? private HttpServletRequest req = null; // multipart/form-dataXg private List<FileItem> fileItemList = null; // Abv??[ht@C?<t@C, Abv??[h> private HashMap<String, Boolean> upFileMap = null; /** * RXgN^ * @param req NGXg * @param outPath ?o?pX */ public FileUpload(HttpServletRequest req, String outPath) { super(); setSizeMax(-1); // TCY setSizeThreshold(1024); // obt@TCY setRepositoryPath(outPath); // ?tH_ setHeaderEncoding("utf-8"); // GR?[fBO this.req = req; this.outPath = outPath; } /** * NGXgp??[^? * multipart/form-data?NGXg? * s??nullp * @return ?NGXg?MAP<L?[, l> */ @SuppressWarnings("unchecked") public HashMap<String, String[]> getRequestParam() { if (fileItemList == null) { try { fileItemList = (List<FileItem>) parseRequest(req); } catch (FileUploadException e) { e.printStackTrace(); return null; } } HashMap<String, String[]> reqParamMap = new HashMap<String, String[]>(); for (FileItem fItem : fileItemList) { // ?tB?[h???iNGXgp??[^lz???j if (fItem.isFormField()) { String key = fItem.getFieldName(); String val = fItem.getString(); if (key != null && !key.equals("")) { reqParamMap.put(key, new String[] { val }); } } } return reqParamMap; } /** * t@CAbv??[h * multipart/form-datat@CAbv??[h * s??nullp * @return Abv??[ht@C?MAP<t@C, Abv??[h> */ @SuppressWarnings("unchecked") public HashMap<String, Boolean> doUpload() { if (fileItemList == null) { try { fileItemList = (List<FileItem>) parseRequest(req); } catch (FileUploadException e) { e.printStackTrace(); return null; } } upFileMap = new HashMap<String, Boolean>(); for (FileItem fItem : fileItemList) { // t@CtB?[h?? if (!fItem.isFormField()) { String key = ""; boolean val = false; // t@C?ipX????j String filePath = (fItem.getName() != null) ? fItem.getName() : ""; // t@C?imt@C?j String fileName = (new File(filePath)).getName(); int pos = fileName.lastIndexOf("\\"); fileName = fileName.substring(pos + 1); pos = fileName.lastIndexOf("/"); fileName = fileName.substring(pos + 1); // t@CAbv??[h if (!fileName.equals("")) { key = fileName; File upFile = new File(outPath + "/" + fileName); try { fItem.write(upFile); val = true; } catch (Exception e) { e.printStackTrace(); upFile.delete(); } } upFileMap.put(key, val); } } return upFileMap; } /** * FileItem?? * t@CAbv??[hWfBXN?A * Xg?[W?t@CACe???B * FileItemCX^XKx?[WRNVXg?[W???A * ?\bhf?m??{?B * Smultipart/form-data??o?B */ public void deleteFileItem() { if (fileItemList != null) { for (FileItem fItem : fileItemList) { fItem.delete(); } } } /** * Abv??[ht@CS?? */ public void deleteAllFile() { String fileName; boolean upResult; File targetFile; for (Map.Entry<String, Boolean> e : upFileMap.entrySet()) { fileName = e.getKey(); upResult = e.getValue(); targetFile = new File(outPath + "/" + fileName); if (upResult && targetFile.exists()) { targetFile.delete(); } } } /** * Abv??[ht@C?? * @param fileName ???t@C */ public void deleteFile(String fileName) { File targetFile = new File(outPath + "/" + fileName); if (targetFile.exists()) { targetFile.delete(); } } }