Java tutorial
/** * Copyright (C) 2013 wancheng <wancheng.com.cn@gmail.com> * * 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. */ package or.tango.server.controller; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import or.tango.server.service.FileService; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class FileController extends BaseController { public void upload(HttpServletRequest request, HttpServletResponse response) { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(4 * 1024); String tmpdir = request.getSession().getServletContext().getRealPath("/temp/"); File tempd = new File(tmpdir); if (!tempd.exists()) { tempd.mkdirs(); } factory.setRepository(tempd); ServletFileUpload upload = new ServletFileUpload(factory); Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddkkmmssSSS"); String filedir = sdf.format(date); String result = null; try { FileItem item = upload.parseRequest(request).get(0); /** * @author ladd_cn(ladd.cn@gmail.com) * @version 1.0 * @date 2013/8/23 * @modify 1:start */ String fileDirectory = request.getSession().getServletContext().getRealPath("/images/" + filedir); if (!isPicture(item.getName(), null)) { System.out.println("Illegel file type"); result = null; } else { File dir = new File(fileDirectory); if (!dir.exists()) { dir.mkdirs(); } File file = new File(fileDirectory + "/" + item.getName()); /** * @modify 1:end */ item.write(file); result = FileService.pickupText(file); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } BaseController.resonseJson(response, result); } public void test(HttpServletRequest request, HttpServletResponse response) { String html = "<html><head><title>upload test</title></head>" + "<body><form action='api?action=File&method=upload' method='post' enctype='multipart/form-data'>" + "<input type='file' name='p'/><input type='submit'/></form>" + "</body></html>"; BaseController.resonseHtml(response, html); } public static boolean isPicture(String pInput, String pImgeFlag) throws Exception { // ??? if (pInput == null) { // ?? return false; } // ??? String tmpName = pInput.substring(pInput.lastIndexOf(".") + 1, pInput.length()); // ??? String imgeArray[][] = { { "bmp", "0" }, { "dib", "1" }, { "gif", "2" }, { "jfif", "3" }, { "jpe", "4" }, { "jpeg", "5" }, { "jpg", "6" }, { "png", "7" }, { "tif", "8" }, { "tiff", "9" }, { "ico", "10" } }; // ???? for (int i = 0; i < imgeArray.length; i++) { // ?? if (!(pImgeFlag == null) && imgeArray[i][0].equals(tmpName.toLowerCase()) && imgeArray[i][1].equals(pImgeFlag)) { return true; } // ?? if ((pImgeFlag == null) && imgeArray[i][0].equals(tmpName.toLowerCase())) { return true; } } return false; } }