jsp实现文件下载例子|jsp实现文件的下载

① jsp实现下载功能

你的下载是一个超链接,你应该<a href="这里写你写下载操作的Action或其他控制下载的组件">下载</a>,你只能同过超链接跳到Action然后Action里面写一些东西,我这是用Struts写的你可以参考一下:public ActionForward fileDownLoad(ActionMapping mapping, HttpServletRequest request, HttpServletResponse response) throws ServletException { String fileName = null;// 名称 String realpath = "D:/crmSite/cdoc/";//定义路径 realpath = "D:/crmSite/cdoc/" + adform.getAdMat().substring(1); realpath = StrUtils.replace(realpath, "\\", "/"); BufferedInputStream bis = null; BufferedOutputStream bos = null; OutputStream fos = null; InputStream fis = null; fileName = realpath.substring(realpath.lastIndexOf("/") + 1, realpath.length()); //System.out.println(realpath); try { response.setContentType(this.getContentType(fileName)); response.setHeader("Content-disposition", "attachment;filename=" + fileName); fis = new FileInputStream(realpath); bis = new BufferedInputStream(fis); fos = response.getOutputStream(); bos = new BufferedOutputStream(fos); int bytesRead = 0; byte[] buffer = new byte[5 * 1024]; while ((bytesRead = bis.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead);// 将文件发送到客户端 } bos.close(); bis.close(); fos.close(); fis.close(); } catch (IOException e) { response.reset(); e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } if (bos != null) { bos.close(); } if (fis != null) { fis.close(); } if (bis != null) { bis.close(); } } catch (IOException e) { System.err.print(e); } } return null; }是否可以解决您的问题?

② jsp 如何实现文件上传和下载功能

上传:

MyjspForm mf = (MyjspForm) form;// TODO Auto-generated method stub

FormFile fname=mf.getFname();

byte [] fn = fname.getFileData();

OutputStream out = new FileOutputStream("D:\"+fname.getFileName());

Date date = new Date();

String title = fname.getFileName();

String url = "d:\"+fname.getFileName();

Upload ul = new Upload();

ul.setDate(date);

ul.setTitle(title);

ul.setUrl(url);

UploadDAO uld = new UploadDAO();

uld.save(ul);

out.write(fn);

out.close();

下载:

DownloadForm downloadForm = (DownloadForm)form;

String fname = request.getParameter("furl");

FileInputStream fi = new FileInputStream(fname);

byte[] bt = new byte[fi.available()];

fi.read(bt);

//设置文件是下载还是打开以及打开的方式msdownload表示下载;设置字湖集,//主要是解决文件中的中文信息

response.setContentType("application/msdownload;charset=gbk");

//文件下载后的默认保存名及打开方式

String contentDisposition = "attachment; filename=" + "java.txt";

response.setHeader("Content-Disposition",contentDisposition);

//设置下载长度

response.setContentLength(bt.length);

ServletOutputStream sos = response.getOutputStream();

sos.write(bt);

return null;

③ 我想用jsp语言编写的网页中实现文件上传、下载的功能,请问完整的代码怎么写,尽量是最简单的,谢谢

去网络搜 jsp 上传文件 jsp下载文件的例子。很简单

④ jsp中怎么实现文件下载

要加上应用来 的路径:自<a href="<%=request.getContextPath()%>/temp/a.doc" > ——————————-还报错?那就是你文件的问题,看看文件大小写和路径是否正确!

⑤ jsp页面如何实现下载文档

jsp页面下载文档是在jsp中有一个a标签 ,当用户点击a标签的时候下载文件。一般采用href属性直接指向一个服务器地址,只要链接的文件存在,就会给出弹出保存对话框.点击a标签 先执行onclick事件,再请求href中指向的地址。前端jsp:<a href="#" onclick="javascript:downloadtest('${app.id}')" id="pluginurl" style="color: #83AFE2;text-decoration:underline;"></a>然后在js中: function downloadtest(id){var url = "<%=request.getContextPath()%>/app/download" + "/" + id;$("#pluginurl").attr("href",url);}后台处理下载逻辑的java代码:/*** 下载文件* @param id appid* @param response*/@RequestMapping(value="/download/{id}")public void download(@PathVariable String id, HttpServletResponse response){String filepath = "";Result result = appService.getAppById(id);App app = (App) result.getMap().get("app");if(app == null){return;}filepath = app.getUrl();File file = new File(filepath);InputStream inputStream = null;OutputStream outputStream = null;byte[] b= new byte[1024];int len = 0;try {inputStream = new FileInputStream(file);outputStream = response.getOutputStream();response.setContentType("application/force-download");String filename = file.getName();filename = filename.substring(36, filename.length());response.addHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));response.setContentLength( (int) file.length( ) );while((len = inputStream.read(b)) != -1){outputStream.write(b, 0, len);}} catch (Exception e) {e.printStackTrace();}finally{if(inputStream != null){try {inputStream.close();inputStream = null;} catch (IOException e) {e.printStackTrace();}}if(outputStream != null){try {outputStream.close();outputStream = null;} catch (IOException e) {e.printStackTrace();}}}}

⑥ jsp如何实现下载dbf文件

1.最直接最简单的,方式是把文件地址直接放到html页面的一个链接中。这样做的缺点是把文件在服务器上的路径暴露了,并且还无法对文件下载进行其它的控制(如权限)。这个就不写示例了。 2.在服务器端把文件转换成输出流,写入到response,以response把文件带到浏览器,由浏览器来提示用户是否愿意保存文件到本地。(示例如下) <% response.setContentType(fileminitype); response.setHeader("Location",filename); response.setHeader("Cache-Control", "max-age=" + cacheTime); //filename应该是编码后的(utf-8) response.setHeader("Content-Disposition", "attachment; filename=" + filename); response.setContentLength(filelength); OutputStream outputStream = response.getOutputStream(); InputStream inputStream = new FileInputStream(filepath); byte[] buffer = new byte[1024]; int i = -1; while ((i = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, i); } outputStream.flush(); outputStream.close(); inputStream.close(); outputStream = null;%>3.既然是JSP的话,还有一种方式就是用Applet来实现文件的下载。不过客户首先得信任你的这个Applet小程序,由这个程序来接受由servlet发送来的数据流,并写入到本地。 servlet端示例 public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType(" text/plain "); OutputStream outputStream = null; try { outputStream = res.getOutputStream(); //把文件路径为srcFile的文件写入outputStream中 popFile(srcFile, outputStream)) ; } catch (IOException e) { e.printStackTrace(); } }JApplet端示例 URLConnection con; try { //url是被调用的SERVLET的网址 如 *.do con = url.openConnection(); con.setUseCaches(false); con.setDoInput(true); con.setDoOutput(true); con.setRequestProperty("Content-Type", "application/octet-stream"); InputStream in = con.getInputStream(); ProgressMonitorInputStream pmInputStream = new ProgressMonitorInputStream(pane, "正在从服务器下载文件内容", in); ProgressMonitor pMonitor = pmInputStream.getProgressMonitor(); pMonitor.setMillisToDecideToPopup(3); pMonitor.setMillisToPopup(3); //localfilepath本地路径,localstr文件文件夹,filename本地文件名 String localfilepath = localstr + filename ; //方法saveFilsaveFilee是把输入流pmInputStream写到文件localfilepath中 if(saveFilsaveFilee(localfilepath,pmInputStream)){ openLocalFile(localfilepath); }4.顺便把JApplet上传文件的代码也贴上来. JApplet端示例 URLConnection con; try { con = url.openConnection(); //url是被调用的SERVLET的网址 如 *.do con.setUseCaches(false); con.setDoInput(true); con.setDoOutput(true); con.setRequestProperty("Content-Type","application/octet-stream"); OutputStream out = con.getOutputStream(); //localfilepath本地路径,localstr文件文件夹,filename本地文件名 String localfilepath = localstr + filename; //文件getOutputStream是把文件localfilepath写到输出流out中 getOutputStream(localfilepath,out); InputStream in = con.getInputStream(); return true; }catch (IOException e) { System.out.println("文件上传出错!"); e.printStackTrace(); }servlet端代码示例 public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType(" text/plain "); InputStream inputStream = null; try { inputStream = res.getInputStream();//把输入流inputStream保存到文件路径为srcFile的文件中 writefile(srcFile, inputStream); } catch (IOException e) { e.printStackTrace(); } } // end service总结:在文件的传输中是流的形式存在的,在硬盘上是文件的形式存在的。我们要做的只是通过HttpServletRequest和HttpServletResponse,或者是response和request来发送流和读取流。以及把文件转换成流或把流转换成文件的操作。

⑦ jsp实现文件下载

你要通过js来获取路径然后再通过js来向磁盘中写文件在实际应用中是不会这么来做的,因为这样既要设置浏览器的安全性还要使用activeX控件。最简单的方法就是发送请求,通过java流向client传送数据,这样浏览器自动会弹出保存的页面的。

⑧ jsp如何实现文件上传与下载

如果服务器端程序使用的是struts2框架的话,我会,其他的不会。struts2:对于上传,jsp页面只需要有个file类型的表单域,如<input type="file" name="xxx" />struts2的接收请求的action中再写三个属性与这个表单域的名称对应起来,他们是,File类型的xxx,String类型的xxxFileName,String类型的xxxContentType,并其设置相应的set/get方法。则框架负责接收上传文件的字节流,解析文件名,文件类型,直接使用即可。对于下载,只需要在action的配置文件中设置如下返回值类型和相应参数:<result type="stream"><param name="contentType">application/octet-stream</param><param name="inputName">inputStream</param><param name="contentDisposition">attachment;filename=xxx </param> xxx为下载文件的文件名</result>且在action总写一个返回值类型为InputStream的getInputStream方法,此方法返回你要下载的文件的流即可。 ps:其中contentDisposition的配置信息中attachment代表点击下载时浏览器先弹出个保存位置的提示框,然后再决定是否下载,默认是inline,即直接打开文件。

⑨ 用jsp怎么编写文件下载代码

下面是我写的一个小例子,下载远程文件urlString,到本地文件localFile.成功返回True,不成功返回False.把这代码插入到你JSP中用到的地方就OK了:) public boolean downLoadFile(String urlString, String localFile) { URL url; byte[] buffer = new byte[512]; int size = 0; boolean success = false; try { url = new URL(urlString); BufferedInputStream stream = new BufferedInputStream(url.openStream()); FileOutputStream fos = new FileOutputStream(localFile); while ((size = stream.read(buffer)) != -1) { fos.write(buffer, 0, size); } fos.close(); stream.close(); success = true; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return success; }

⑩ jsp实现文件的下载

你要通过js来获取路径然后再通过js来向磁盘中写文件在实际应用中是不会这么来做的,因为内这样既要容设置浏览器的安全性还要使用activeX控件。最简单的方法就是发送请求,通过java流向client传送数据,这样浏览器自动会弹出保存的页面的。


赞 (0)