javaweb上传文件(sevlet)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > javaweb上传文件(sevlet)

javaweb上传文件(sevlet)

 2013/8/27 0:57:15  wuhaocn  程序员俱乐部  我要评论(0)
  • 摘要:把file标签与其他标签分开只处理file标签并把其他参数重定向到另外的函数或servletpublicclassD_BookServletextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;privateD_BookServiced_BookService=newD_BookServiceImpl();@Overrideprotectedvoidservice(HttpServletRequestrequest
  • 标签:javaweb Web 上传 文件 Java 上传文件
把file标签与其他标签分开只处理file标签并把其他参数重定向到另外的函数或servlet
class="java" name="code">

 public class D_BookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private D_BookService d_BookService = new D_BookServiceImpl();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		System.out.println(method);
		if ("add".equals(method)) {
		    upLoadFile(request,  response);
		}else if("addbook".equals(method)){
			addBook(request, response);
		}else if("edit".equals(method)){
			showBook(request, response);
		}
        
	}
       //添加图书
	private void addBook(HttpServletRequest request, HttpServletResponse response) throws IOException{
		  D_Book d_Book = new D_Book();
		  String product_pic  = request.getParameter("product_pic");
		  String product_name  = request.getParameter("product_name");   // varchar(100) NOT NULL,-- 商品名称
		  String fixed_price   = request.getParameter("fixed_price");    // double NOT NULL,-- 商品定价
		  String dang_price    = request.getParameter("dang_price");     //double NOT NULL,-- 当当价
		  String author        = request.getParameter("author") ;        //varchar(200) NOT NULL,-- 作者
		  String publishing    = request.getParameter("publishing");     //varchar(200) NOT NULL,-- 出版社
		  String publish_time  = request.getParameter("publish_time");   //date NOT NULL,-- 出版时间
		  String author_summary= request.getParameter("author_summary"); // varchar(100) NOT NULL,-- 作者简介
		  String description   = request.getParameter("description");    //varchar(100) default NULL,-- 商品描述
		  d_Book.setProduct_pic(product_pic);
		  d_Book.setProduct_name(product_name);
		  d_Book.setDang_price(Double.parseDouble(dang_price));
		  d_Book.setFixed_price(Double.parseDouble(fixed_price));
		  d_Book.setAuthor(author);
		  d_Book.setPublishing(publishing);
		  d_Book.setPublish_time(Date.valueOf(publish_time));
		  d_Book.setAuthor_summary(author_summary);
		  d_Book.setDescription(description);	
		  d_Book.setScore(1);
		  d_BookService.insertD_Book(d_Book);
		  response.sendRedirect("/DangDang/Admin/jspfile/editbook.jsp");
	}
	//上传文件
	@SuppressWarnings("unchecked")
	public void upLoadFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		 // 在解析请求之前先判断请求类型是否为文件上传类型
		  boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		  String url = "";
		  if(isMultipart){
		      // 文件上传处理工厂
		      FileItemFactory fileItemFactory = new DiskFileItemFactory(); 
		      // 创建文件上传处理器
		      ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
		      // 开始解析请求信息
			  try {
					List<FileItem> fileItems = servletFileUpload.parseRequest(request);
			        // 对所有请求信息进行判断
				    for(FileItem fileItem: fileItems){
				     //如果提交信息为表单格式设置为新的url
				       if (fileItem.isFormField()) {
			                    String fieldName = fileItem.getFieldName();
			                    String value = fileItem.getString();
				    	        url += "&" + fieldName + "=" + value;
			           }
				       //如果提交信息为文件格式
			           else {
			                    String fileName = fileItem.getName();
			                    String basePath = System.getProperty("user.dir").replace("\\", "/")  + "/../webapps/DangDang/BookImage/";
			                    File file = new File(basePath, fileName);
			                    if (!file.exists()) {
			                       file.createNewFile(); 
			    				}
			                    fileItem.write(file);
			                    url += "&product_pic"  + "=" + "BookImage/" + fileName;
			           }
				    } 
				} catch (FileUploadException e1) {
						e1.printStackTrace();
				} catch (Exception e) {
						e.printStackTrace();
				}
//把设置好的url重新转发到servlet或函数
              response.sendRedirect("/DangDang/admindbook?method=addbook" + url);		           
		  }
 
	}
   
}

发表评论
用户名: 匿名