commons_fileupload实现文件上传

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		DiskFileUpload fu = new DiskFileUpload();
		fu.setSizeMax(2 * 1024 * 1024);
		fu.setSizeThreshold(4096);
		fu.setRepositoryPath("c:\\temp");
		try {
			List fileItems = fu.parseRequest(request);
			Iterator iter = fileItems.iterator();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();
				//忽略其他不是文件域的所有表单信息
				if (!item.isFormField()) {
					String name = item.getName();
					name = name.substring(name.lastIndexOf("\\") + 1);
					System.out.println(name);
					File f = new File("D:\\UploadTest\\" + name);
					item.write(f);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		out.close();
	}

你可能感兴趣的:(html,C++,c,F#,C#)