日期:2014-05-16  浏览次数:20322 次

JSP直接读取用户上传的文件
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			DefaultFileItemFactory factory = new DefaultFileItemFactory();
			FileUpload sevletFileUpload = new FileUpload(factory);
			// 设置允许用户上传文件大小,单位:字节,这里设为2m
			sevletFileUpload.setSizeMax(2 * 1024 * 1024);
			// 设置最多只允许在内存中存储的数据,单位:字节
			factory.setSizeThreshold(4096);
			// 开始读取上传信息
			List fileItems = sevletFileUpload.parseRequest(request);
			FileItem item = (FileItem)fileItems.get(1);
			FileItem item2 = (FileItem)fileItems.get(0);
			String company = item2.getString();
                        //处理你的逻辑。。。
			response.setCharacterEncoding("gb2312");
			response.getWriter().write("操作完成!请仔细检查上传数据是否正确!");
			}
		    catch(DocumentErrorException dee){
		    	dee.printStackTrace();
		    	response.getWriter().write("有错误发生:数据格式不正确!");
		    }catch (Exception ex) {
				ex.printStackTrace();
				response.getWriter().write("有错误发生:"+ex.getMessage());
			}
	}

?这里用到了? apache的? fileupload.jar包。。。