12

    <form method="post" action="UpLoadServlet" enctype="multipart/form-data">    上传用户:<input type="text" name="username"/><br/>    上传文件:<input type="file" name="file" title="上传文件"/><hr/>    <input type="submit" value="确定"/>    </form> public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int pageNo = 1; String flag = "f"; String pageNoStr = request.getParameter("pno"); if(pageNoStr != null && !pageNoStr.equals("")){ pageNo = Integer.parseInt(pageNoStr); flag = request.getParameter("flag"); } FilePagePro filePagePro = new FilePagePro(); List<UpedFile> fileList = filePagePro.getFilePagedList(pageNo, flag,3); RequestDispatcher rd = request.getRequestDispatcher("filelist.jsp"); request.setAttribute("fileList", fileList); request.setAttribute("pno", String.valueOf(pageNo)); rd.forward(request, response); }public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String filepath = "upload"; String name = ""; String value = ""; String fileName = ""; String contentType = ""; PrintWriter out = response.getWriter(); boolean isMultipart = ServletFileUpload.isMultipartContent(request); if(!isMultipart){ out.print("非上传操作!"); return; } out.println(isMultipart + "<br/>"); DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(1024*1024); factory.setRepository(new File("d:\\tmp"));  ServletFileUpload upload = new ServletFileUpload(factory);  upload.setHeaderEncoding("utf-8"); upload.setSizeMax(20*1024*1024);  List<FileItem> items = null; try { items = upload.parseRequest(request); } catch (FileUploadException e) { out.print("出错了!"); return; Iterator<FileItem> iter = items.iterator();  while (iter.hasNext()) {  FileItem item = (FileItem) iter.next(); if (item.isFormField()) {  name = item.getFieldName(); value = item.getString("utf-8"); out.print(name + ":" + value + "<br/>"); } else {  fileName = item.getName();  contentType = item.getContentType(); System.out.println(contentType); int start = fileName.lastIndexOf("\\"); fileName = fileName.substring(start + 1); out.print(fileName); File uploadedFile = new File(this.getServletContext().getRealPath("/upload"),fileName);  try { item.write(uploadedFile); } catch (Exception e) { e.printStackTrace(); }
    <form method="post" action="UpLoadServlet" enctype="multipart/form-data">
    	上传用户:<input type="text" name="username"/><br/>
    	上传文件:<input type="file" name="file" title="上传文件"/><hr/>
    	<input type="submit" value="确定"/>
    </form>

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		int pageNo = 1;
		String flag = "f";
		String pageNoStr = request.getParameter("pno");
		if(pageNoStr != null && !pageNoStr.equals("")){
			pageNo = Integer.parseInt(pageNoStr);
			flag = request.getParameter("flag");
		}
		FilePagePro filePagePro = new FilePagePro();
		List<UpedFile> fileList = filePagePro.getFilePagedList(pageNo, flag,3);
		RequestDispatcher rd = request.getRequestDispatcher("filelist.jsp");
		request.setAttribute("fileList", fileList);
		request.setAttribute("pno", String.valueOf(pageNo));
		rd.forward(request, response);
	}
public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String filepath = "upload";
		String name = "";
		String value = "";
		String fileName = "";
		String contentType = "";
		PrintWriter out = response.getWriter();
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if(!isMultipart){
			out.print("非上传操作!");
			return;
		}
		out.println(isMultipart + "<br/>");
		
		DiskFileItemFactory factory = new DiskFileItemFactory();
		
		factory.setSizeThreshold(1024*1024);
		factory.setRepository(new File("d:\\tmp")); 
	
		ServletFileUpload upload = new ServletFileUpload(factory); 
		
		upload.setHeaderEncoding("utf-8");
		
		upload.setSizeMax(20*1024*1024); 
		
		List<FileItem> items = null;
		try {
			items = upload.parseRequest(request);
		} catch (FileUploadException e) {
			out.print("出错了!");
			return;
		} 
		
		Iterator<FileItem> iter = items.iterator(); 
		while (iter.hasNext()) { 
			FileItem item = (FileItem) iter.next();
			if (item.isFormField()) { 
				name = item.getFieldName();
				value = item.getString("utf-8");
				out.print(name + ":" + value + "<br/>");
			} else { 

				fileName = item.getName(); 
				contentType = item.getContentType();

				System.out.println(contentType);
				int start = fileName.lastIndexOf("\\");
				fileName = fileName.substring(start + 1);

				out.print(fileName);
				File uploadedFile = new File(this.getServletContext().getRealPath("/upload"),fileName); 
				try {
					item.write(uploadedFile);
				} catch (Exception e) {
				
					e.printStackTrace();
				} 
				
			} 
		}


阅读更多

没有更多推荐了,返回首页