java - 关于excel上传解析
PHP中文网
PHP中文网 2017-04-18 10:27:52
0
4
431

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

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(4)
伊谢尔伦

You try the example on the official website.

巴扎黑

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

刘奇

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

刘奇

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...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!