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);
	}
}

좋은 웹페이지 즐겨찾기