全球主机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 64|回复: 0

高防服务器读取jar包中resource文件夹下的文件154.223.64.2租用

[复制链接]
发表于 2020-3-19 12:24:18 | 显示全部楼层 |阅读模式
高防服务器读取jar包中resource文件夹下的文件154.223.64.2租用



需要服务器咨询以下联系方式
联系人:銳輝(香港)科技 敏敏
扣扣:3007425289/2853898501
电话:18316411879

前两天写了对医院端提供的接口,接口需要从数据库中主动抽取符合条件的数据,然后将数据替换到resource文件夹下已有的XML模板中,例如模板在resource文件夹的templete文件夹中,模板名称为publicTemplete.xml,刚开始使用了File file = ResourceUtils.getFile("classpath:templates/publicTemplete.xml"),然后将获取到的xml模板通过SAXReader解析成Document文件,在通过Document的asXML方法转换成字符串返回给调用者,代码如下:

private String returnXmlStr() throws FileNotFoundException, UnsupportedEncodingException, IOException {
        File file = ResourceUtils.getFile("classpath:templates/publicTemplete.xml");
        SAXReader reader = new SAXReader();
        Document doc = reader.read(file);               
        return doc.asXML();
}
上述代码在未发布成Jar包前通过eclipse启动springboot项目,是可以使用的,然而等到发布成Jar包后通过java -jar xxx.jar来启动时,控制台就会报异常:class path resource [templates/publicTemplete.xml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/ydhl/xxx。通过百度发现从Jar包中读取文件需要使用ClassPathResource类的getInputStream()方法来完成,代码如下:

private String returnXmlStr() throws FileNotFoundException, UnsupportedEncodingException, IOException {
        //此处如果用File file = Resource.getFile(filePath)会报异常:找不到文件
            Resource resource = new ClassPathResource("classpath:templates/publicTemplete.xml");
            InputStream is = resource.getInputStream();
            ByteArrayOutputStream result = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) != -1) {
                    result.write(buffer, 0, length);
            }
            return result.toString(StandardCharsets.UTF_8.name());
        }
再次通过java -jar xxx.jar启动Jar包,读取成功,特此记录下来,已备查寻和增加印象。


202.46.38.1
202.46.38.2
202.46.38.3
202.46.38.4
202.46.38.5
202.46.38.6
202.46.38.7
202.46.38.8
202.46.38.9
202.46.38.10
202.46.38.11
202.46.38.12
202.46.38.13
202.46.38.14
202.46.38.15
202.46.38.16
202.46.38.17
202.46.38.18
202.46.38.19
202.46.38.20
202.46.38.21
202.46.38.22
202.46.38.23
202.46.38.24
202.46.38.25
202.46.38.26
202.46.38.27
202.46.38.28
202.46.38.29
202.46.38.30
202.46.38.31
202.46.38.32
202.46.38.33
202.46.38.34
202.46.38.35
202.46.38.36
202.46.38.37
202.46.38.38
202.46.38.39
202.46.38.40
202.46.38.41
202.46.38.42
202.46.38.43
202.46.38.44
202.46.38.45
202.46.38.46
202.46.38.47
202.46.38.48
202.46.38.49
202.46.38.50
202.46.38.51
202.46.38.52
202.46.38.53
202.46.38.54
202.46.38.55
202.46.38.56
202.46.38.57
202.46.38.58
202.46.38.59
202.46.38.60
202.46.38.61
202.46.38.62
202.46.38.63
202.46.38.64
202.46.38.65
202.46.38.66
202.46.38.67
202.46.38.68
202.46.38.69
202.46.38.70
202.46.38.71
202.46.38.72
202.46.38.73
202.46.38.74
202.46.38.75
202.46.38.76
202.46.38.77
202.46.38.78
202.46.38.79
202.46.38.80
202.46.38.81
202.46.38.82
202.46.38.83
202.46.38.84
202.46.38.85
202.46.38.86
202.46.38.87
202.46.38.88
202.46.38.89
202.46.38.90
202.46.38.91
202.46.38.92
202.46.38.93
202.46.38.94
202.46.38.95
202.46.38.96
202.46.38.97
202.46.38.98
202.46.38.99
202.46.38.100
202.46.38.101
202.46.38.102
202.46.38.103
202.46.38.104
202.46.38.105
202.46.38.106
202.46.38.107
202.46.38.108
202.46.38.109
202.46.38.110
202.46.38.111
202.46.38.112
202.46.38.113
202.46.38.114
202.46.38.115
202.46.38.116
202.46.38.117
202.46.38.118
202.46.38.119
202.46.38.120
202.46.38.121
202.46.38.122
202.46.38.123
202.46.38.124
202.46.38.125
202.46.38.126
202.46.38.127
202.46.38.128
202.46.38.129
202.46.38.130
202.46.38.131
202.46.38.132
202.46.38.133
202.46.38.134
202.46.38.135
202.46.38.136
202.46.38.137
202.46.38.138
202.46.38.139
202.46.38.140
202.46.38.141
202.46.38.142
202.46.38.143
202.46.38.144
202.46.38.145
202.46.38.146
202.46.38.147
202.46.38.148
202.46.38.149
202.46.38.150
202.46.38.151
202.46.38.152
202.46.38.153
202.46.38.154
202.46.38.155
202.46.38.156
202.46.38.157
202.46.38.158
202.46.38.159
202.46.38.160
202.46.38.161
202.46.38.162
202.46.38.163
202.46.38.164
202.46.38.165
202.46.38.166
202.46.38.167
202.46.38.168
202.46.38.169
202.46.38.170
202.46.38.171
202.46.38.172
202.46.38.173
202.46.38.174
202.46.38.175
202.46.38.176
202.46.38.177
202.46.38.178
202.46.38.179
202.46.38.180
202.46.38.181
202.46.38.182
202.46.38.183
202.46.38.184
202.46.38.185
202.46.38.186
202.46.38.187
202.46.38.188
202.46.38.189
202.46.38.190
202.46.38.191
202.46.38.192
202.46.38.193
202.46.38.194
202.46.38.195
202.46.38.196
202.46.38.197
202.46.38.198
202.46.38.199
202.46.38.200
202.46.38.201
202.46.38.202
202.46.38.203
202.46.38.204
202.46.38.205
202.46.38.206
202.46.38.207
202.46.38.208
202.46.38.209
202.46.38.210
202.46.38.211
202.46.38.212
202.46.38.213
202.46.38.214
202.46.38.215
202.46.38.216
202.46.38.217
202.46.38.218
202.46.38.219
202.46.38.220
202.46.38.221
202.46.38.222
202.46.38.223
202.46.38.224
202.46.38.225
202.46.38.226
202.46.38.227
202.46.38.228
202.46.38.229
202.46.38.230
202.46.38.231
202.46.38.232
202.46.38.233
202.46.38.234
202.46.38.235
202.46.38.236
202.46.38.237
202.46.38.238
202.46.38.239
202.46.38.240
202.46.38.241
202.46.38.242
202.46.38.243
202.46.38.244
202.46.38.245
202.46.38.246
202.46.38.247
202.46.38.248
202.46.38.249
202.46.38.250
202.46.38.251
202.46.38.252
202.46.38.253
202.46.38.254
202.46.38.255



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|小黑屋|全球服务器论坛

GMT+8, 2025-2-1 19:48 , Processed in 1.450802 second(s), 19 queries , File On.

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表