文件上传java1

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html,charset=utf-8");
		Boolean ismu=ServletFileUpload.isMultipartContent(request);
		if(ismu) {
			FileItemFactory ff=new DiskFileItemFactory();
			ServletFileUpload sUpload=new ServletFileUpload(ff);
			try {
				List<FileItem> gg=sUpload.parseRequest(request);
				Iterator<FileItem> jj=gg.iterator();
				int jk=1;
				while(jj.hasNext()) {
					FileItem hh=jj.next();
					if((hh.getFieldName()).equals("file")) {
						String filename=hh.getName();
						String path=request.getSession().getServletContext().getRealPath("uploadone");
						File file=new File(path,filename);
						hh.write(file);
					}else if((hh.getFieldName()).equals("name")) {
						jk=Integer.parseInt(hh.getString());
						System.out.print(jk);
					}
				}
				
				
				
				
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
注意:将两个jar包放入项目webcontent/WEB-INF/lib中【commons-fileupload-1.3.1、commons-io-2.4】
在webcontent中新建一个存放文件的目录。

猜你喜欢

转载自www.cnblogs.com/qinyios/p/10169145.html