search

Home  >  Q&A  >  body text

java - 关于excel上传解析

前台通过form表单上传文件,后台通过request.getInputStream得到流,但是POI建立wordbook要求是fileInputStream,我获取的是servletInputStream,强转会出错。怎么解决。我查的资料都是通过固定文件位置读取流,我想使用用户上传的流来生成workbook该怎么做?

PHP中文网PHP中文网2818 days ago464

reply all(4)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:29:52

    You try the example on the official website.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:29:52

    1. There is a solution to save it to a certain location on the server and then read it. You need to pay attention to the unique file name and delete it after processing.
    2. That’s not right. You can get a file by uploading it. It is a File object. I forgot how to do it

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:29:52

    What version of POI? In the construction method of XSSFWorkbook, one parameter is obviously InputStream

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:29:52

    ServletInputStream extends InputStream Of course, forced conversion cannot be done. You can read ServletInputStream from the buffer and then convert it to InputStream (if you don’t mind it). The normal way is to create a temporary file on the server (if you can’t see it) You can delete it)
    You can try using springmvc,
    1.@RequestParam(value="file",required = false)MultipartFile file //Receive parameters
    2. But you also need to build a file saving path

    String path = request.getSession(true).getServletContext().getRealPath("/upload");
     String fileName = new Date().getTime()+file.getOriginalFilename();

    3.Save the file

    File targetFile = new File(path,fileName);
    file.transferTo(targetFile)

    4.POI...

    reply
    0
  • Cancelreply