文件上传 servlet 从HttpServletRequest.getInputStream()中获得消息内容
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件上传 servlet 从HttpServletRequest.getInputStream()中获得消息内容相关的知识,希望对你有一定的参考价值。
前段时间采用spring谢了文件上传,但是跟手机端调试时,发现他们的插件只能使用原生的数据流的形式上传文件,根据HTTP的规范看了下上传文件的消息体,写了如下的方法,供大家使用:
@Note(note = "bigAnt:servlet上传文件", author = "zhangwenbo") @RequestMapping("modifyPictureByStream2") @ResponseBody public SimpleMessage<Object> modifyPictureByStream2(HttpServletRequest req, HttpServletResponse response) { logger.info("modifyHeaderPicture has invoked.filename is "); SimpleMessage<Object> sm = new SimpleMessage<Object>(); BufferedReader bufferReader = null; ByteArrayOutputStream outputStream = null; FileOutputStream fileout = null; try { // String boundary = // req.getContentType().substring(req.getContentType().indexOf("boundary=") // + 9); // 从流中获取消息 bufferReader = new BufferedReader(new InputStreamReader(req.getInputStream())); outputStream = new ByteArrayOutputStream(); String line = bufferReader.readLine(); boolean contentBegin = false; String filename = ""; while (true) { if (null != line && !"".equals(line.trim())) { int filename_index = line.indexOf("filename=\""); if (filename_index > -1) { contentBegin = true;// 准备开始读取文件内容 filename = line.substring(filename_index + 10, line.length() - 1); logger.info("filename is " + filename); // 跳过两行,直接到文件的内容 line = bufferReader.readLine(); line = bufferReader.readLine(); } logger.info("filename_index :" + filename_index); } else { if (contentBegin) { break;// 跳出循环 } } if (contentBegin) { logger.info("line is :" + line); outputStream.write(line.getBytes()); } logger.info("read next line "); line = bufferReader.readLine(); } int filename_split = filename.indexOf("."); // 本地创建临时文件 File fileIO = File.createTempFile(filename.substring(0, filename_split), filename.substring(filename_split)); if (!fileIO.exists()) { fileIO.createNewFile(); } // 写文件 fileout = new FileOutputStream(fileIO); fileout.write(outputStream.toByteArray()); fileout.flush(); Map<String, Object> postBody = new HashMap<String, Object>(); postBody.put("loginname", req.getSession().getAttribute(BigAntHelper.SESSION_USER_ID)); // 发送文件 BigAntBaseRes uploadResult = BigAntClient.getInstance().uploadPicture(postBody, filename, fileIO); logger.info("upload return msg is " + uploadResult); if (null != uploadResult) { if ("1".equals(uploadResult.getStatus().trim())) { sm.set("data", uploadResult.getData()); } else { sm.set("message", uploadResult.getMsg()); } } else { sm.set("message", "upload fail"); } // 不管成功还是失败,删除临时文件 if (fileIO.exists()) { fileIO.delete(); } } catch (IOException e) { logger.info("upload file fail.", e); } finally { try { if (null != bufferReader) { bufferReader.close(); } if (null != outputStream) { outputStream.close(); } if (null != fileout) { fileout.close(); } } catch (Exception e2) { logger.info("close inputStream fail.", e2); } } return sm; } |
仅供参考!
本文出自 “java” 博客,请务必保留此出处http://zwbjava.blog.51cto.com/2789897/1789242
以上是关于文件上传 servlet 从HttpServletRequest.getInputStream()中获得消息内容的主要内容,如果未能解决你的问题,请参考以下文章
文件上传 servlet 从HttpServletRequest.getInputStream()中获得消息内容