§ Fox平台文件下载
§ 1.新建服务类并添加下载方法
package service.server.customer;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map;
import service.common.base.BaseBusiService;
import service.common.bean.JsonRequest;
import service.common.bean.JsonResponse;
import cn.com.bankit.phoenix.communication.http.HttpMessage;
import cn.com.bankit.phoenix.resource.ResourceManager;
public class DownloadService extends BaseBusiService{
/**
*
*/
public DownloadService() {
super();
// TODO Auto-generated constructor stub
}
public HttpMessage downloadFile(HttpMessage httpMessage) throws Exception{
Map<String,Object> reqMap = (Map<String,Object>)httpMessage.getContent();
ResourceManager resMg = ResourceManager.getInstance();
//TODO 加入自己的处理逻辑
String path = (String) reqMap.get("path");
String installRoot = resMg.getInstallRoot();
//以取平台服务根目录下的文件为例:
StringBuilder sb = new StringBuilder();
sb.append(installRoot);
sb.append("/");
sb.append(path);
File file = new File(sb.toString());
InputStream in = new BufferedInputStream(new FileInputStream(file));
HttpMessage respMsg = new HttpMessage();
respMsg.setHeader("Content-Type","application/octet-stream");
respMsg.setContent(in);
return respMsg;
}
@Override
public JsonResponse execute(JsonRequest request) throws Exception {
// TODO Auto-generated method stub
return null;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
§ 2.新建服务配置文件并添加服务路径
<package name="download" namespace="download" >
<!--下载文件 -->
<service name="downloadFile" class="service.server.customer.DownloadService" method="downloadFile"></service>
</package>
1
2
3
4
2
3
4
§ 3.调用方法
- 引入fox.core jar包
- 使用 fox.core.comm.http 中的 HttpRequester 类的 directDownload() 方法来下载文件
public static void main(String[] args) throws Exception{
System.out.println("开始连接..........");
Map<String,String> reqMap = new HashMap<String,String>();
//path对应为需要下载的文件路径和文件名 与服务端downloadFile方法中的path对应
reqMap.put("path", "startup.sh");
HttpRequester httpRequester =HttpRequester.getDefault();
//该方法中的参数依次是 下载地址(服务) 请求参数 本地保存路径 超时时间(ms)
//fox_pad替换为自己的项目 /download/download/downloadFile.do替换为自己的服务路径
httpRequester.directDownload("http://101.34.102.67:9700/fox_pad/download/download/downloadFile.do",reqMap,new File("E:\\debug.sh"),60000);
System.out.println("结束下载");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
← Fox3.0 新特性 文件断点上传 →