zoukankan      html  css  js  c++  java
  • 文件下载

    1.Controller

    package com.bypx.controller;

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.UnsupportedEncodingException;
    import java.util.UUID;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

    import org.apache.commons.io.FileUtils;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.multipart.commons.CommonsMultipartFile;

    @Controller
    @RequestMapping("FileController")
    public class FileController {
    /***
    * 实现文件的上传
    *
    * @RequestParam:指定客户端发送数据的名字
    * **/
    @RequestMapping("/uploadFile")
    public String uploadFile(@RequestParam("cmf") CommonsMultipartFile cmf,
    HttpSession session) {
    // 1获得上传的文件内容
    byte[] bytes = cmf.getBytes();
    // 2获得upload的绝对路径
    String path = session.getServletContext().getRealPath("/upload");
    // 3在服务器的upload目录下创建File对象
    String oname = cmf.getOriginalFilename(); // 上传文件的原始名字
    String uuid = UUID.randomUUID().toString();
    File file = new File(path, uuid
    + oname.substring(oname.lastIndexOf(".")));
    // 4将上传的文件拷贝到指定的目录
    try {
    FileUtils.writeByteArrayToFile(file, bytes);
    } catch (IOException e) {
    e.printStackTrace();
    }
    return "index.jsp";
    }

    /***
    * 实现文件下载
    *
    * @throws UnsupportedEncodingException
    * ***/
    @RequestMapping("/download")
    public String downloadFile(@RequestParam("fileName") String fileName,
    HttpServletRequest request, HttpServletResponse response) {
    if (fileName != null) {
    String realPath = request.getSession().getServletContext()
    .getRealPath("upload");
    File file = new File(realPath, fileName);

    if (file.exists()) {
    byte[] buffer = new byte[1024];
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    try {
    response.setContentType("application/force-download");// 设置强制下载不打开
    response.addHeader(
    "Content-Disposition",
    "attachment;fileName="
    + new String(fileName.getBytes("gbk"),
    "iso-8859-1"));// 设置文件名

    fis = new FileInputStream(file);
    bis = new BufferedInputStream(fis);
    OutputStream os = response.getOutputStream();
    int i = bis.read(buffer);
    while (i != -1) {
    os.write(buffer, 0, i);
    i = bis.read(buffer);
    }
    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    } finally {
    if (bis != null) {
    try {
    bis.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if (fis != null) {
    try {
    fis.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }
    }
    return null;
    }
    }

    2.html(这个按钮,就是安一下就会下载)

    {field:'fname', title:'附件',align:'center',
    formatter : function(value, rows, index) {
    return "<a href='/jxc/FileController/download.do?fileName="+rows.fname+"' ><b>查看</b></a>";
    }
    },

    3.SpringMvc.xml配置文件中再加上一个就可以了

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <property name="defaultEncoding" value="utf-8"></property><!-- 客户端发送数据的编码 -->
    <property name="maxUploadSize" value="5242880"></property><!-- 上传文件的大小 -->
    <property name="uploadTempDir" value="/upload"></property>
    </bean>

  • 相关阅读:
    OC UITextField只允许输入两位小数
    UIBezierPath使用
    2020-11-25:go中,map的底层数据结构是什么?
    2020-11-24:n个物品每个物品都有一定价值,分给2个人,怎么分两个人的价值差最小?
    2020-11-23:go中,s是一个字符串,s[0]代表什么?是否等于固定字节数?
    2020-11-22:mysql中,什么是filesort?
    2020-11-21:java中,什么是跨代引用?
    2020-11-20:java中,听说过CMS的并发预处理和并发可中断预处理吗?
    2020-11-19:go中,defer原理是什么?
    2020-11-18:java中,到底多大的对象会被直接扔到老年代?
  • 原文地址:https://www.cnblogs.com/likeji/p/6282752.html
Copyright © 2011-2022 走看看