When I was doing my graduation project recently, I needed to put files (mainly pictures and videos) on the web page. Because I don't want the project to be too bloated, in line with the idea of separating data and code, I put the files in the non-project directory (disk directory), and the database is also stored in the disk directory (such as G:\XX\XXX..). In this way, some problems are encountered when reading, and the solution is recorded as follows:
(1) Image reading Use the <img> tag to display the image
in the jsp page, and its src attribute supports streaming, so the solution is to write a servlet , use the input stream to read the image, and then use the response stream to output. Write the address of the servlet in src The
servlet code is as follows:
The jsp page is as follows:
video player uses flowplayer. For the detailed use of flowplayer, see
[FlowPlayer parameter description_ Hou Xiaowei][1]
href cannot write the disk path, use the virtual path to solve this problem The configuration of the
virtual path is as follows:
In tomact Add the following to the <host> tag in the server.xml file:
[1]: https://www.cnblogs.com/microstep/p/4972997.html
(1) Image reading Use the <img> tag to display the image
in the jsp page, and its src attribute supports streaming, so the solution is to write a servlet , use the input stream to read the image, and then use the response stream to output. Write the address of the servlet in src The
servlet code is as follows:
ReadFileFromDisk.servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); //filepath is the disk path of the image String filepath = request.getParameter("filepath"); String transpath = filepath.replace("\\", "/"); FileInputStream fis = new FileInputStream(transpath); OutputStream os = response.getOutputStream(); try { int count = 0; byte[] buffer = new byte[1024 * 1024]; while ((count = fis.read(buffer)) != -1) os.write(buffer, 0, count); } catch (IOException e) { e.printStackTrace (); } finally { if (os != null) os.close(); if (fis != null) fis.close(); } }
The jsp page is as follows:
<img src="ReadFileFromDisk?filepath=${filepath}" alt="Load failed"/>(2) Video reading The
video player uses flowplayer. For the detailed use of flowplayer, see
[FlowPlayer parameter description_ Hou Xiaowei][1]
href cannot write the disk path, use the virtual path to solve this problem The configuration of the
virtual path is as follows:
In tomact Add the following to the <host> tag in the server.xml file:
<Context path="/file" docBase="G:\profile\video\" debug="0" reloadable="true"/>That is, G:\\profile\\video\\ is mapped to /file, so if you access G:\\profile\\video\\a.mp4, you can write in href: /file/a.mp4
[1]: https://www.cnblogs.com/microstep/p/4972997.html