|
|
|
| 采用HttpServlet 实现web文件下载 |
编辑:rocks 审核:rocks 文章来源:CSDN博客
关键词:serlvet 发表日期:2006-02-23 17:13:42 浏览次数:4762次 |
|
|
|
|
本文版权归原作者,中国JAVA手机网收录本文的目的是让更多人阅读到此文章。转载请注明出处为中国JAVA手机网<www.cnjm.net>
来自:http://www.cnjm.net/tech/article956.html [转载于CSDN博客]
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import com.TopWisdom.framework.util.*;
public class WebDownLoad extends HttpServlet { public WebDownLoad() { }
JAVA手机网[www.cnjm.net] private ServletConfig config;
public void init(ServletConfig config) throws ServletException { super.init(config); this.config = config; }
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException { doGet(req,res); } //取得附件的名称 public static String getAttachName(String file_name) { if(file_name==null) return ""; file_name = file_name.trim(); int iPos = 0; iPos = file_name.lastIndexOf("\\"); if(iPos>-1){ file_name = file_name.substring(iPos+1);
JAVA手机网[www.cnjm.net] } iPos = file_name.lastIndexOf("/"); if(iPos>-1){ file_name = file_name.substring(iPos+1); } iPos = file_name.lastIndexOf(File.separator); if(iPos>-1){
JAVA手机网[www.cnjm.net] file_name = file_name.substring(iPos+1); }
JAVA手机网[www.cnjm.net] return file_name; } //UTF8转码 public static String toUtf8String(String s) { StringBuffer sb = new StringBuffer(); for (int i=0;i<s.length();i++) {
JAVA手机网[www.cnjm.net] char c = s.charAt(i); if (c >= 0 && c <= 255) { sb.append(c); } else { byte[] b; try { b = Character.toString(c).getBytes("utf-8"); } catch (Exception ex) { System.out.println(ex); b = new byte[0]; }
JAVA手机网[www.cnjm.net] for (int j = 0; j < b.length; j++) { int k = b[j]; if (k < 0) k += 256; sb.append("%" + Integer.toHexString(k).toUpperCase()); } } } String s_utf8 = sb.toString(); sb.delete(0,sb.length()); sb.setLength(0); sb = null; return s_utf8; } //取得下载文件的真实全路径名称
JAVA手机网[www.cnjm.net] private String getRealName(HttpServletRequest request,String file_name) { if(request==null || file_name==null) return null; file_name = file_name.trim(); if(file_name.equals("")) return null; String file_path = request.getRealPath(file_name); if ( file_path== null) return null; File file = new File(file_path); if (!file.exists()) return null;
JAVA手机网[www.cnjm.net] return file_path; } //实现下载 public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException { String file_name = request.getParameter("file_name"); if(file_name==null) file_name = ""; file_name = file_name.trim();
JAVA手机网[www.cnjm.net] InputStream inStream= null; String attch_name = ""; byte[] b = new byte[100];
JAVA手机网[www.cnjm.net] int len= 0; try { //取得附件的名称 attch_name = getAttachName(file_name); file_name = getRealName(request,file_name);
JAVA手机网[www.cnjm.net] if(file_name==null) { System.out.println("文件不存在,或者禁止下载");
JAVA手机网[www.cnjm.net] return ; }
JAVA手机网[www.cnjm.net] attch_name = toUtf8String(attch_name); //读到流中 inStream=new FileInputStream(file_name); //设置输出的格式
JAVA手机网[www.cnjm.net] response.reset(); response.setContentType("application/x-msdownload"); response.addHeader("Content-Disposition","attachment; filename=\"" + attch_name + "\""); //循环取出流中的数据 while((len=inStream.read(b)) >0) { response.getOutputStream().write(b,0,len); } inStream.close(); }catch ( Exception e ){ if ( e instanceof java.io.FileNotFoundException ) { try { response.sendRedirect("/tip/file_not_found.html"); } catch ( IOException ex ) { ex.printStackTrace(System.err); } } else { e.printStackTrace(System.err); } } }
}
来自:http://www.cnjm.net/tech/article956.html
|
|
|
|
|
|
相关文章
暂无相关文章
|
|
| 最新评论
|
|
|
|