コード例 #1
0
	public File doAttachment(HttpServletRequest request)
			throws ServletException, IOException {
		File file = null;
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		try {
			List<?> items = upload.parseRequest(request);
			Iterator<?> itr = items.iterator();
			while (itr.hasNext()) {
				FileItem item = (FileItem) itr.next();
				if (item.isFormField()) {
					parameters
							.put(item.getFieldName(), item.getString("UTF-8"));
				} else {
					File tempFile = new File(item.getName());
					file = new File(sc.getRealPath("/") + savePath, tempFile
							.getName());
					item.write(file);
				}
			}
		} catch (Exception e) {
			Logger logger = Logger.getLogger(SendAttachmentMailServlet.class);
			logger.error("邮件发送出了异常", e);
		}
		return file;
	}
コード例 #2
0
ファイル: User.java プロジェクト: vladotrocol/website
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    try {
      List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
      for (FileItem item : items) {
        if (item.isFormField()) {
          // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
          String fieldname = item.getFieldName();
          String fieldvalue = item.getString();
          // ... (do your job here)
        } else {
          // Process form file field (input type="file").
          String fieldname = item.getFieldName();
          String filename = FilenameUtils.getName(item.getName());
          InputStream filecontent = item.getInputStream();
          // ... (do your job here)
        }
      }
    } catch (FileUploadException e) {
      throw new ServletException("Cannot parse multipart request.", e);
    }

    // ...
  }