| |
| 用Jsp来实现文件下载功能的几种方式 |
| |
发布者: 发布时间:2007-09-23 |
|
|
|
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来发送流和读取流。以及把文件转换成流或把流转换成文件的操作。
|
| (转载文章请保留出处:北天JAVA技术网(www.java114.com)) |
| |
| 更多精彩文章: |
| 如何解决JSP页面显示乱码问题 |
| 实现JSP数据和JavaScript数据交互使用 |
| 关于JSP/Servlet中的重定向技术的综述 |
| 经验分享全过程JSP程序员完全蜕变手册 |
| JSP+JavaBean+Servlet工作原理实例讲解 |
| 用SAX的代码生成器降低SAX解析器的繁琐 |
| |
| 最近评论: |
|
|
| 春暖花开 |
| wow power leveling2
wow power leveling gvf
wow power levelingfcvg
wow power leveling
wow power leveling
wow powerlevelingfcgv
wow powerlevelingwq4
wow powerleveling
wow powerleveling
wow powerleveling
world of warcraft power leveling
world of warcraft power leveling
world of warcraft power leveling
world of warcraft power levelingszr
world of warcraft power leveling
world of warcraft powerleveling
world of warcraft powerlevelinge5t
world of warcraft powerleveling
world of warcraft powerleveling
world of warcraft powerleveling
wow gold
wow gold
wow goldrfy5
wow gold
wow gold
world of warcraft goldre
world of warcraft goldgh
world of warcraft gold
world of warcraft goldfr
world of warcraft gold
AOC Power Levelinggvgv
AGE OF CONAN Power Levelingllwwbb928
n7y6m7my |
|
|
| 鍥炲 |
|
|
|
| 如果真的有来生! |
| 四川旅游,九寨沟旅游,稻城亚丁旅游,四姑娘山旅游,海螺沟旅游,西藏旅游,
max(2385) |
|
|
| 那天的情景! |
| Maple Story mesos,MapleStory mesos,ms mesos,mesos,SilkRoad Gold,
max(76) |
|
|
| 那天的情景! |
| Maple Story mesos,MapleStory mesos,ms mesos,mesos,SilkRoad Gold,
max(9646) |
|
|
| 轻轻走过你的窗前! |
| world of warcraft gold,cheap world of warcraft gold,warcraft gold,world of warcraft gold,cheap world of warcraft gold,warcraft gold, max(6963) |
|
|
| 轻轻走过你的窗前! |
| world of warcraft gold,cheap world of warcraft gold,warcraft gold,world of warcraft gold,cheap world of warcraft gold,warcraft gold max(3730) |
|
|
| 不在的哪天! |
| final fantasy xi gil,final fantasy xi gil,final fantasy xi gil,final fantasy xi gil,
max(8655) |
|
|
| 没有情人的情人节! |
| wow gold,wow power leveling.wow power leveling,wow power leveling,
max(7518) |
|
|
| 没有情人的情人节! |
| wow gold,wow power leveling.wow power leveling,wow power leveling,
max(8652) |
|
|
| |
| 免责声明:该文章由网友发表,如果对您造成侵权,请联系站长。 |
|