windows live Writer test

來源:互聯網
上載者:User

標籤:boolean   toolbar   string   inline   float   6.4   pad   tco   extends   

package com.newegg.shopping.util.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.*;import java.util.*;import java.util.regex.*;import java.io.*;import org.apache.commons.fileupload.servlet.*;import org.apache.commons.fileupload.disk.DiskFileItemFactory;public class FileUpload extends HttpServlet { //BLOB@Overridepublic void init(ServletConfig config) throws ServletException {this.config = config;}private ServletConfig config = null;//private String uploadPath= "D:\\";//private File tempPath = new File("D:\\upload\\temp\\"); // 用於存放臨時檔案的目錄private File tempPath = new File("D:\\temp\\");public void destroy() {config = null;super.destroy();}public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {int id = -1;String uploadPath = config.getInitParameter("uploadPath"); // 用於存放上傳檔案的目錄res.setContentType("text/html; charset=utf-8");PrintWriter out = res.getWriter();System.out.println(req.getContentLength());System.out.println(req.getContentType());DiskFileItemFactory factory = new DiskFileItemFactory();// maximum size that will be stored in memoryfactory.setSizeThreshold(4096);// the location for saving data that is larger than getSizeThreshold()//factory.setRepository(tempPath);factory.setRepository(tempPath);//factory.setRepository(new File("D:\\temp\\"));ServletFileUpload upload = new ServletFileUpload(factory);// maximum size before a FileUploadException will be thrownupload.setSizeMax(1000000);try {List fileItems = upload.parseRequest(req);// assume we know there are two files. The first file is a small// text file, the second is unknown and is written to a file on// the serverIterator iter = fileItems.iterator();// 正則匹配,過濾路徑取檔案名稱String regExp = ".+\\\\(.+)$";// 過濾掉的檔案類型String[] errorType = { ".exe", ".com", ".cgi", ".jsp" };Pattern p = Pattern.compile(regExp);while (iter.hasNext()) {FileItem item = (FileItem) iter.next();if(item.isFormField()) {if(item.getFieldName().equals("id")) {id = Integer.parseInt(item.getString());}}// 忽略其他不是檔案域的所有表單資訊if (!item.isFormField()) {String name = item.getName();long size = item.getSize();if ((name == null || name.equals("")) && size == 0)continue;Matcher m = p.matcher(name);boolean result = m.find();if (result) {for (int temp = 0; temp < errorType.length; temp++) {if (m.group(1).endsWith(errorType[temp])) {throw new IOException(name + ": wrong type");}}try {// 儲存上傳的檔案到指定的目錄// 在下文中上傳檔案至資料庫時,將對這裡改寫item.write(new File(uploadPath + id + ".jpg"));//item.write(new File("d:\\) + m.group(1));out.print(name + "&nbsp;&nbsp;" + size + "<br>");} catch (Exception e) {out.println(e);}} else {throw new IOException("fail to upload");}}}} catch (IOException e) {out.println(e);} catch (FileUploadException e) {out.println(e);}}}

windows live Writer test

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.