当前位置: 代码网 > it编程>编程语言>Java > RestTemplate上传、下载文件实例代码

RestTemplate上传、下载文件实例代码

2025年02月13日 Java 我要评论
上传上传文件分两种①要处理的文件本身存在当前文件系统下②要处理的文件只是个临时文件,或者说是文件流的形式存在在以下代码中会分别标注private static void upattach() {

上传

上传文件分两种

  • ①要处理的文件本身存在当前文件系统下
  • ②要处理的文件只是个临时文件,或者说是文件流的形式存在

在以下代码中会分别标注

private static void upattach() {
    string uploadurl = "http://192.168.1.10/upload";
    string appid = "";
    string accesskey = "";
    string filepath = "";
    string originalfilename = "test.xlsx";
    
    long currenttimemillis = system.currenttimemillis();
    string timemillis = string.valueof(currenttimemillis);
    // 文件路径
    string base64key = base64.encodebase64string(filepath.getbytes());
    string sign = getsign(appid, base64key, timemillis, accesskey);

    multivaluemap<string, object> params = new linkedmultivaluemap<>(3);
    
    // @deprecated 
    // write to temporary file and then process, need to be deleted and tends to generate too much garbage
    // path tempfile = files.createtempfile("upload-file", ".txt");
    // files.write(tempfile, file.getbytes());
    // params.add("file", new filesystemresource(tempfile.tofile()));

	// ============================== 这里是重点 ==============================
    // ① 文件存在,直接获取文件输入流即可
	file file = new file("c:\\test.xlsx");
    params.add("file", new inputstreamresource(file.getinputstream(), originalfilename));

	// ② 临时文件,如multipartfile,获取其字节流来处理
	multipartfile multipartfile = null;
	byte[] bytearray = multipartfile.getbytes();
    bytearrayresource bytearrayresource = new bytearrayresource(bytearray) {
        @override
        public string getfilename() {
        	// 这里不指定文件名的话,上传过去的文件名会乱码
            return originalfilename;
        }
    };
    params.add("file", bytearrayresource);
	// ========================================================================

	// 其他参数按需采用	
    // key为文件路径
    params.add("key", filepath);

    httpheaders headers = new httpheaders();
    headers.setcontenttype(mediatype.multipart_form_data);
    headers.add("x-fs-timestamp", timemillis);
    headers.add("x-fs-appid", appid);
    headers.add("x-fs-sign", sign);
    httpentity<multivaluemap<string, object>> requestentity = new httpentity<>(params, headers);

    resttemplate resttemplate = springutils.getbean(resttemplate.class);
    resttemplate.postforobject(uploadurl, requestentity, jsonobject.class);
}


/** 加密 **/
private static string getsign(string appid, string base64key, string timestamp, string accesskey) {
    string sign = appid + "|" + base64key + "|" + timestamp;
    return new hmacutils(hmacalgorithms.hmac_sha_256, accesskey).hmachex(sign);
}

下载

下载只需要注意返回值为byte[]即可:

// resource为文件唯一标识,按需采用
public static final byte[] dwattach(string resource) {
    string appid = "";
    string accesskey = "";

    long currenttimemillis = system.currenttimemillis();
    string timemillis = string.valueof(currenttimemillis);
    string base64key = base64.encodebase64string(resource.getbytes());
    string sign = getsign(appid, base64key, timemillis, accesskey);

    string downloadurl = "http://192.168.1.10?appid={appid}&timestamp={timestamp}&sign={sign}";
    hashmap<string, object> urivariables = new hashmap<>(5);
    urivariables.put("appid", appid);
    urivariables.put("timestamp", timemillis);
    urivariables.put("sign", sign);
    resttemplate resttemplate = springutils.getbean(resttemplate.class);

    return resttemplate.getforobject(downloadurl, byte[].class, urivariables);
}

后续可通过new bytearrayinputstream(byte[])转换成流,或者org.springframework.util.filecopyutils, org.apache.commons.io.fileutils等工具类,进行其他处理。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com