weblogic 获取文件路径 下载文件

今日遇到一个问题:做了一个文件下载的功能,本机装的是Tomcat,测试的时候能运行过,但是发布到weblogic上面的时候就一直报错:未找到文件。

百度过后发现:

/**
* 文件下载
* 文件放在webapp/lssi/fileresoure 下面,
* 如:publicBusniess下面的征地农转非登记表,则在fileresoure 下面新建publicbusniess文件夹,并且将征地农转非登记表.xls 放在该目录
* 在具体Action里面调用时格式为:download("publicBusiness/征地农转非登记表.xls");
* @param filePath 文件路径及文件名
* @return
* @author chencj
* @throws Exception 
*/

public String downloadFile(String filePath) throws Exception{

//不能使用ServletActionContext.getServletContext().getRealPath("/")获取文件路径
//程序打包发布到weblog上之后,getRealPath返回的是null。该项目不存在所谓的文件系统中的目录,在tomcat下面发布打包应用的时候,它会把包解压,所以即使都是打包

//项目发布上去tomcat上面还是可以支持getRealPath().
//String fullFilePath1 =ServletActionContext.getServletContext().getRealPath("/")+"lssi/fileresource/"+filePath;
//String fullFilePath1 = request.getSession().getServletContext().getResource("lssi/fileresource/"+filePath).getFile();
//String path=this.getClass().getClassLoader().getResource("/").getPath();
String fullFilePath = this.getClass().getClassLoader().getResource("/").getPath();
//this.getClass().getClassLoader().getResource("/").getPath()取出来的路径中空格会转换为%20这种在文件目录中是不能被识别的,所以需要重新编码为utf-8
fullFilePath = URLDecoder.decode(fullFilePath,"utf-8");
//this.getClass().getClassLoader().getResource("/").getPath()获取出来的路径是到WEB-INF/classes下面的路径,但是weblog可能会存在对此目录访问权限控制,所以文件最好不要放在该目录下面。自己处理获取需要的目录
fullFilePath = fullFilePath.replace("WEB-INF/classes/", "")+"lssi/fileresource/"+filePath;
        /*读取文件*/
        File file = new File(fullFilePath);
        /*如果文件存在*/
        if (file.exists()) {
            String filename = URLEncoder.encode(file.getName(), "utf-8");
            response.reset();
           //设置下载文件的类型
            response.setContentType("application/x-msdownload");
          //添加文件名字
            response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
            //添加文件的大小信息
            int fileLength = (int) file.length();
            response.setContentLength(fileLength);
            /*如果文件长度大于0*/
            if (fileLength != 0) {
                /*创建输入流*/
                InputStream inStream = new FileInputStream(file);
                byte[] buf = new byte[4096];
              //获得输出网络流
                ServletOutputStream servletOS = response.getOutputStream();
                int readLength =0;
                while (((readLength = inStream.read(buf)) != -1)) {
                    servletOS.write(buf, 0, readLength);
                }
                inStream.close();
                servletOS.flush();
                servletOS.close();
            }
        }else{
        throw new AppException(fullFilePath+"  未找到!");
        }
        return null;
}

   如果仅仅提供下载的情况  也可以选择使用 getResourceAsStream()返回的是一个InputStream 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值