团队开发day09
经过和朱同学的讨论,最后终于找到了一个很好的解决图片和其他信息不能同时传送的问题。
就是其他信息也用流的方式接受,这样接受图片的时候也可以接受到其他信息的信息。
servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //通知客户端以utf-8编码进行解析 response.setContentType("text/html;charset=utf-8"); //数据库以UTF-8编码解析数据 request.setCharacterEncoding("UTF-8"); String method = request.getParameter("method"); if(method.equals("addGoods")) { information good = new information(); //判断请求是否为multipar请求 if(!ServletFileUpload.isMultipartContent(request)) { throw new RuntimeException("当前请求不支持文件上传"); } //为基于磁盘的文件项创建一个FileItem工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); //设置临时文件的边界值,大于该值时,上传文件会先保存在临时文件中,否则,上传文件将直接写入到内存 //单位:字节,设置边界值1M,一字节=1024M; factory.setSizeThreshold(1024*1024*1); //设置文件临时储存 String temppath=this.getServletContext().getRealPath("/temp"); File temp=new File(temppath); factory.setRepository(temp); //创建一个新的文件上传处理程序 ServletFileUpload upload = new ServletFileUpload(factory); //设置每一个item的头部字符编码,其可以解决文件名中文乱码问题; upload.setHeaderEncoding("UTF-8"); //设置单个文件的最大边界值(这里是2M) upload.setFileSizeMax(1024*1024*2); //设置一次上传所有文件总和的最大值(对上传多个文件起作用,这里最大为5M) upload.setSizeMax(1024*1024*5); System.out.println("this"); //解析请求,获取所有的item try { //调用ServletFileUpload.parseRequest方法解析request对象, //得到一个保存了所有上传内容的List对象。 List <FileItem> items = upload.parseRequest(request); //遍历 int count = 0; for(FileItem item:items){ //若item为普通表单项 count++; System.out.println(count); if(item.isFormField()){ //获取表单中属性名称 System.out.println("this"); String fieldName = (String)item.getFieldName(); if(fieldName.equals("name")){name=item.getString("UTF-8"); good.setName(name); } if(fieldName.equals("tagname")) {tagname=item.getString("UTF-8");good.setTagname(tagname);} if(fieldName.equals("price")) {price=item.getString("UTF-8"); double pricetem = Double.parseDouble(price); good.setPrice(pricetem);} if(fieldName.equals("category")) {category=item.getString("UTF-8");good.setCategory(category);} if(fieldName.equals("date")) { date=item.getString("UTF-8");good.setDate(date);} if(fieldName.equals("num")) {num=item.getString("UTF-8"); int numtem=Integer.parseInt(num); good.setNum(numtem);} if(fieldName.equals("context")) { context=item.getString("UTF-8");good.setContent(context);} }else{ //获取文件大小 long size=item.getSize(); //获取文件类型 String contentType = item.getContentType(); //获取上传文件原始名字 String fileName = item.getName(); System.out.println("文件大小:"+size); System.out.println("文件的类型:"+contentType); //截取后缀 String postfix = fileName.substring(fileName.lastIndexOf(".") + 1).trim().toLowerCase(); Dao temdao = new Dao(); int id = temdao.getMaxId(); id+=1; //设置goods类pic名 good.setPicture(Integer.toString(id)); fileName=id+".jpg";//+postfix; System.out.println("文件的名称:"+fileName); //获取输入流,其实有上传文件的内容 InputStream inputStream = item.getInputStream(); String path="C:\\Users\\kai\\eclipse-workspace\\Marketserver\\WebContent\\allpicture"; File dirFile=new File(path); if(!dirFile.exists()){ //创建多级目录mkdirs() dirFile.mkdir(); } BufferedImage bufferedImage; try { //read image file bufferedImage = ImageIO.read(item.getInputStream()); // create a blank, RGB, same width and height, and a white background BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB); //TYPE_INT_RGB:创建一个RBG图像,24位深度,成功将32位图转化成24位 newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null); // write to jpeg file ImageIO.write(newBufferedImage, "jpg", new File(path+"//"+fileName)); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } inputStream.close(); //删除临时文件 item.delete(); } } //添加新商品 Dao dao = new Dao(); dao.add(good); System.out.println("添加成功!"); request.getRequestDispatcher("addsucss.jsp").forward(request,response); } catch (FileUploadException e) { e.printStackTrace(); } }else if(method.equals("deleteGoods")) { } }