javaweb使用smartupload下载文件,和在线打开文件

使用smartupload下载文件,必须使用 smartupload.jar包,参考博客:http://blog.csdn.net/tan313/article/details/40212147


jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <title>下载示例</title>
  </head>
  
  <body>
  
   <a href="${pageContext.request.contextPath }/servlet/downloadServlet?name=background.jpg">word文档下载</a>     
   <a href="${pageContext.request.contextPath }/servlet/onlineOpenServlet?id=wen.txt">在线打开</a>
 
  </body>
</html>

downloadServlet:

package com.ning.servlet;


import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class downloadServlet extends HttpServlet {

	//文件下载功能
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获取下载文件名
	    String fileName = request.getParameter("name");
	    System.out.println(fileName);
	    
	    //新建一个smartUpload对象
	    SmartUpload smartUpload = new SmartUpload();
	    //初始化
	    smartUpload.initialize(this.getServletConfig(), request, response);
	    //设定contentDisposition为null以禁止浏览器自动打开文件
	    //保证单击链接后是下载文件。
	    smartUpload.setContentDisposition(null);
	    //下载文件
	    try {
	      smartUpload.downloadFile("/download/"+fileName);//使用相对路径进行下载
	    } catch (SmartUploadException e) {
	      e.printStackTrace();
	    }
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}

在线打开:downloadServlet:


package com.ning.servlet;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class onlineOpenServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String filename = request.getParameter("id");
		System.out.println(filename);
		
	//	String filePath = "/download/"+filename;
		String filePath = "E:\\Workspaces\\MyEclipse 10\\.metadata\\.me_tcat\\webapps\\Ning\\download\\"+filename;
		File file = new File(filePath);
		if(!file.exists()){
			 response.sendError(404, "File not found!");
	            return;
		}
		BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
        byte[] buf = new byte[1024];
        int len = 0;

        response.reset(); // 非常重要
        
        //在线打开
        URL u = new URL("file:///" + filePath);
        response.setContentType(u.openConnection().getContentType());
        response.setHeader("Content-Disposition", "inline; filename=" + file.getName());
        
        //下载
/*        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());*/
        
        OutputStream out = response.getOutputStream();
        while ((len = br.read(buf)) > 0)
            out.write(buf, 0, len);
        br.close();
        out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}


你可能感兴趣的:(javaweb使用smartupload下载文件,和在线打开文件)