public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("已接收到请求"); //获取前台传过来的流信息 InputStream fileSource = req.getInputStream(); //临时文件存储位置 String tempFileName = "D:/temp/tempFile"; //创建临时文件 File tempFile = new File(tempFileName); //输出流指向临时文件 FileOutputStream outputStream = new FileOutputStream(tempFile); byte b[] = new byte[1024]; int n; //从输入流读取内容写入到输出流 while(( n = fileSource.read(b)) != -1){ outputStream.write(b, 0, n); } //关闭输入、输出流 outputStream.close(); fileSource.close();}
2 回答
已采纳
胜利女神在微笑
TA贡献67条经验 获得超59个赞
DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb factory.setRepository(new File(this.getServletContext().getRealPath("WEB-INF")));// 设置缓冲区目录 ServletFileUpload upload = new ServletFileUpload(factory); List<FileItem> items = upload.parseRequest(request);// 得到所有的文件 Iterator<FileItem> i = items.iterator(); if (!i.hasNext()) { logger.warn("receiveFile ,file not exists!"); return; } FileItem fi = (FileItem) i.next(); fi.getInputStream()
添加回答
举报
0/150
提交
取消