zoukankan      html  css  js  c++  java
  • Java获取Web服务器文件

    Java获取Web服务器文件

    Java获取Web服务器文件

    如果获取的是服务器上某个目录下的有关文件,就相对比较容易,可以设定死绝对目录,但是如果不能设定死绝对目录,也不确定web服务器的安装目录,可以考虑如下两种方式:

    1. 方法一:先获取web服务器当前绝对位置,然后拼接相对目录

    考虑到java web项目一般会采用strust2来做,所以可以使用ServletActionContext.getServletContext().getRealPath("")来获取当前的web服务器绝对路径。再拼接相对位置即可,总的程序示例如下:

    public InputStream getFileStream() throws Exception{
        System.out.println(filename);
        String realPath = ServletActionContext.getServletContext().getRealPath("");
        System.out.println("realPath:"+realPath);
        return new FileInputStream(realPath+filename);
    }
    
    1. 方法二:通过webURL地址才获取文件

    因为不管web服务器安装在什么位置,其对外显示的URL地址总是不变的,如http://xx.xx.xx.xx/xx 那么要返回http://xx.xx.xx.xx/xx/xx.doc 文件的话,可以采用如下方式,没有做过实验,不过类似于网络爬虫中的文件获取。

    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection();  
    BufferedReader reader = new BufferedReader(new InputStreamReader(conn. 
    getInputStream()));  
    

    获取网络流后再返回文件流1

    Date: 2012-11-25 11:51:59 中国标准时间

    Author: csophys

    Org version 7.8.11 with Emacs version 24

    Validate XHTML 1.0
  • 相关阅读:
    js回调函数
    axios如何先请求A接口然后在请求B接口
    蓝桥杯省赛 区间移位(二分+玄学贪心)
    P1403 [AHOI2005]约数研究(筛法)
    P1029 最大公约数和最小公倍数问题(数论水题)
    洛谷P1147连续自然数和(前缀和)
    洛谷P1017进制转换(进制转换/取模)
    洛谷P1088火星人(stl/全排列)
    Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) D. Navigation System(最短路)
    Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) C. Remove Adjacent(贪心+暴力)
  • 原文地址:https://www.cnblogs.com/csophys/p/2787386.html
Copyright © 2011-2022 走看看