您的位置:首页 > 编程语言 > Java开发

javaweb 下载文件

2016-09-24 20:14 169 查看
resp.sendRedirect("网页地址");         网页重定向

// 设置字符串的编码 

req.setCharacterEncoding("utf-8");

resp.setContentType("text/html;charset=utf-8");

resp.setCharacterEncoding("utf-8");

// 写入界面

PrintWriter out = resp.getWriter();

out.print("内容");

// 获得配置文件中共有的那个配置文件 

ServletContext sc = this.getServletContext();

// 苹果字符编码

ios-8859-1

// 下载文档   下载的文件还会乱码还未处理

//JAVA代码

package download;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.swing.filechooser.FileNameExtensionFilter;

public class DownloadSever extends HttpServlet {

    

    @Override

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 呈现了web应用的Servlet视图

        ServletContext sc = this.getServletContext();

// 设置文件格式  

        resp.setCharacterEncoding("utf-8");

        req.setCharacterEncoding("utf-8");

        resp.setContentType("text/html;charset = utf-8");

// 得到HTML文件中的标签的   fileName

        String fileName = req.getParameter("fileName");

// 设置字符编码格式 

        String strname = new String(fileName.getBytes("ISO-8859-1"),"utf-8");

        System.out.println(strname);

//下载不同的文件

        if(strname.equals("AJAX基础.ppt")){

            down(resp,sc,"PPT/AJAX基础.ppt",".ppt");

        }else if(strname.equals("赵丽颖.jpg")){

            down(resp,sc,"image/赵丽颖.jpg",".jpg");

        }

    }

    /**

     * 下载文档   

     * @param resp    响应  

     * @param sc     整个文档公有的对象  

     * @param path2    文件路径   

     * @param form   下载的格式

     */

    public void down(HttpServletResponse resp,ServletContext sc,String path2,String form){

        resp.setHeader("Content-Disposition", "attachment;  filename="+System.currentTimeMillis()+form);

        String path = sc.getRealPath(path2);

        // 得到写入流   

        FileInputStream fis;

        try {

            fis = new FileInputStream(new File(path));

            ServletOutputStream sos= resp.getOutputStream();

            byte[] by = new byte[1024];

            int length = 0;

            while((length =fis.read() )!=-1 ){

                sos.write(by,0,length);

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        

    }

    

    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // TODO Auto-generated method stub

        doGet(req, resp);

    }

// HTML文件 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

    <a href="DownloadSever?fileName=AJAX基础.ppt">下载AJAX基础.ppt</a>

    <a href="DownloadSever?fileName=赵丽颖.jpg">下载赵丽颖图片</a>

</body>

</html>

// 配置文件 

<!-- 下载 -->

 

   <servlet>

    <servlet-name>DownloadSever</servlet-name>

    <servlet-class>download.DownloadSever</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>DownloadSever</servlet-name>

    <url-pattern>/DownloadSever</url-pattern>

  </servlet-mapping>}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: