java 下载文件(web服务器与文件服务器分离)

 

 

   场景一 download from webserver ,source file come from webserver file system

 

 

 

 

/**
 * 文件下载
 */
@Controller
@RequestMapping(value = "file/",method=RequestMethod.GET)
public class FileSvcController extends BaseController {


    @RequestMapping("download")
    public HttpServletResponse download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //web服务器上文件存储路径
            String filePath = "d:\\星空.jpg";
            java.io.File file = new java.io.File(filePath);
            //在浏览器上显示的下载文件名
            String fileName = "星空.jpg";

            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header URLEncoder对文件名编码输出,防止中文文件名出现乱码
            response.addHeader("Content-Disposition", "attachment;filename=" +java.net.URLEncoder.encode(fileName, "UTF-8"));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return response;
        
    }
}

 

 

 

   场景二 download from webserver ,source file come from remote server file system

 

/**
 * 文件下载
 */
@Controller
@RequestMapping(value = "file/",method=RequestMethod.GET)
public class FileSvcController extends BaseController {

    final static int BUFFER_SIZE = 4096;

    @RequestMapping("download")
    public HttpServletResponse download(HttpServletRequest request, HttpServletResponse response){
        InputStream fis = null;
        try {
            String fileName = "星空.jpg";
            String fileUrl = "http://192.168.1.105/2016/11/26/"+URLEncoder.encode("星空.jpg","UTF-8");
            URL uri = new URL(fileUrl);
            InputStream fileStream = uri.openStream();
            // 以流的形式下载文件。
            fis = new BufferedInputStream(fileStream);
            byte[] buffer = InputStreamTOByte(fis);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
            response.addHeader("Content-Length", "" + buffer.length);
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return response;
    }

    /**
     * 循环从inputstream中读取文件流到byte数组
     * 将inputstream convert to byte array
     * @param in
     * @return
     * @throws IOException
     */
    public byte[] InputStreamTOByte(InputStream in) throws IOException{
        ByteArrayOutputStream outStream = null;
        try {
            outStream = new ByteArrayOutputStream();
            byte[] data = new byte[BUFFER_SIZE];
            int count = -1;
            while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
                outStream.write(data, 0, count);

            data = null;
            outStream.close();
            return outStream.toByteArray();
        }catch (IOException e){
            throw  e;
        }
        finally {
            if(outStream != null) outStream.close();
        }
    }
}

 

 

 

   其他

 

 测试的时候是使用自己的家用笔记本电脑(作为文件服务器)和公司配置的笔记本电脑(作为web服务器),自己的家用笔记本电脑作为文件服务器(windows操作系统),自己的笔记本电脑可以使用Http://192.168.1.105/2016/11/26/00c62dc866ce49f8a421602c21ce61f0.pdf访问到文件,但是使用公司的电脑(ip为192.168.1.115)无法访问到Http://192.168.1.105/2016/11/26/00c62dc866ce49f8a421602c21ce61f0.pdf文件,原来是文件服务器开启了防火墙导致的,将文件服务器防火墙关闭即可正常访问文件服务器下载查看文件

 

 

 

 

转载于:https://www.cnblogs.com/huangzhihua/p/6104658.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值