Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to download files from linux server by java

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how java downloads files from the linux server". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to download files from the linux server by java" can help you solve the problem.

First, front-end code 1, html code project file

Plan specification

Service agreement 2, js code / / get the file, mainly from the database: file name, file path function getProjectFiles (type) {var fileParam = {pageNo: 1, pageSize: 10, xmId: ${detail [0] ["Project ID"]}, cxlx: type,} $.ajax ({url: "/ prod/getProjectFiles", type: "POST", data: fileParam, dataType: "JSON" Success: function (ret) {if (ret & & ret ['code'] > 0) {debugger / / rendering interface var dataList = ret [' list'] Download (dataList [0] ["attachment name"], dataList [0] ["attachment path"])} / / download the file and call the backend interface function download (fileName,filePath) {window.open ("/ downloadTwo?fileName=" + encodeURI (fileName) + "& downUrl=" + filePath); return;} 3, css style

All the styles in the front end use the layui architecture.

Second, background code 1. Get the file

The main thing is to call the stored procedure, go to the database to get the file name and file path, and then download it.

/ * * obtain project file * I_CXLX IN NUMBER,-- query type 1 | Trust contract 2 | Trust Plan description 3 | subscription risk statement 5 | Privacy Agreement | 6 user Service Agreement * I_XSXM IN NUMBER-Distribution solution id * @ param request * @ param modelMap * @ return * / @ RequestMapping (value = "/ getProjectFiles" Method = RequestMethod.POST) @ ResponseBody public DataResultSet getProjectFiles (HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {int pageNo = ParamUtils.getInt (request, "pageNo", 1) / / Page number int pageSize = ParamUtils.getInt (request, "pageSize", 10); / / get the number of displayed messages String cxlx = ParamUtils.getString (request, "cxlx", ""); / / query type 1 | Trust contract 2 | Trust Plan statement 3 | subscription risk statement 5 | Privacy Agreement | 6 user Service Agreement String xmId = ParamUtils.getString (request, "xmId", "") / / Distribution scheme id DataResultSet productHistory = prodService.getProjectFiles (pageNo,pageSize,cxlx,xmId); return productHistory;} 2. Start downloading / * attachment download * * @ param request * @ param response * / @ RequestMapping ("/ downloadTwo") public void downloadFileTwo (HttpServletRequest request, HttpServletResponse response) {String fileName = request.getParameter ("fileName") String downUrl = request.getParameter ("downUrl"); goToDownload (request, response, downUrl, fileName) } / * download * * @ param request * @ param response * @ param downUrl download path * @ param fjmc download file name * / private void goToDownload (HttpServletRequest request, HttpServletResponse response, String downUrl, String fjmc) {response.setCharacterEncoding ("UTF-8"); response.setContentType ("application/x-msdownload") Try {String encodenickname = URLEncoder.encode (fjmc, "UTF-8"); / / convert Unicode or ie will garbled response.setHeader ("Content-Disposition", "attachment;fileName=" + new String (encodenickname.getBytes ("UTF-8"), "ISO8859-1")) / / the code noted here is locally tested / / String path = request.getSession (). GetServletContext (). GetRealPath ("/"); / / String ATTACH_PATH= AppConfig.getInstance (). GetProperty ("attach.base", "); / / if (StringUtils.isNotEmpty (ATTACH_PATH)) {/ / path = ATTACH_PATH / /} / / logger.debug ("= path===" + path); File file = new File (downUrl); if (! file.exists ()) {response.sendError (404, "File not found!"); return;} long fileLength = file.length () Response.setHeader ("Content-Length", String.valueOf (fileLength)); BufferedInputStream bis = new BufferedInputStream (new FileInputStream (file)); BufferedOutputStream bos = new BufferedOutputStream (response.getOutputStream ()); byte [] buff = new byte [2048]; int bytesRead While (- 1! = (bytesRead = bis.read (buff, 0, buff.length)) {bos.write (buff, 0, bytesRead);} bis.close (); bos.close ();} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace () Third, effect

Click on the file picture or file directly and you can download it.

So much for the introduction of "how java downloads files from linux server". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report