Java tutorial
/** * Copyright 1996-2013 Founder International Co.,Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * @author ? */ package com.founder.fix.fixflow.explorer.util; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; @SuppressWarnings("unchecked") public class FileHandle { private static List<FileItem> fi = new LinkedList<FileItem>(); public static void whenUploadFileBindParameter(HttpServletRequest request, HttpServletResponse response) throws Exception { try { fi.clear(); fi.removeAll(fi); Iterator<FileItem> iterator = createFactory(request, response); while (iterator.hasNext()) { FileItem fileItem = (FileItem) iterator.next(); if (fileItem.isFormField()) { // ?????? String name = fileItem.getFieldName(); // inputname String value = fileItem.getString(); // input?value request.setAttribute(filterEncoding(name), filterEncoding(value)); } else { fi.add(fileItem); } } } catch (Exception e) { e.printStackTrace(); throw new Exception("??!"); } } public static String updload(HttpServletRequest request, HttpServletResponse response, String autoPath, String showFileName) throws Exception { String message = ""; // ?? File uploadPath = new File(autoPath); if (!uploadPath.exists()) { uploadPath.mkdir(); } for (int i = 0; i < fi.size(); i++) { FileItem fileItem = fi.get(i); if (!fileItem.isFormField()) { //String fieldName = fileItem.getFieldName(); // ????file inputname String fileName = fileItem.getName(); // file input?? //String contentType = fileItem.getContentType(); // //long size = fileItem.getSize(); // String filePath = autoPath + File.separator + showFileName; // org.apache.commons.fileupload.FileItem ??write? // fileItem.write(new File(filePath)); // ???? ? ? OutputStream outputStream = new FileOutputStream(new File(filePath));// ??? InputStream inputStream = fileItem.getInputStream(); int length = 0; byte[] buf = new byte[1024]; while ((length = inputStream.read(buf)) != -1) { // ????? outputStream.write(buf, 0, length); } inputStream.close(); outputStream.close(); message = "(" + fileName + ")?,??:" + filePath; } } return message; } public static Iterator<FileItem> createFactory(HttpServletRequest request, HttpServletResponse response) throws Exception { try { // ?? servletFileUplaod DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); // ?request??inputFileInput List<FileItem> fileItemList = servletFileUpload.parseRequest(request); return fileItemList.iterator(); } catch (Exception e) { e.printStackTrace(); throw new Exception("?!"); } } public static String filterEncoding(String param) throws UnsupportedEncodingException { return new String(param.getBytes("ISO-8859-1"), "utf-8"); } }