SoFunction
Updated on 2025-04-07

RestTemplate upload and download file instance code

Upload

There are two types of upload files

  • ①The file to be processed itself exists under the current file system
  • ②The file to be processed is just a temporary file, or it exists in the form of a file stream.

The following code will be marked separately

private static void upAttach() {
    String uploadUrl = "http://192.168.1.10/upload";
    String appId = "";
    String accessKey = "";
    String filePath = "";
    String originalFilename = "";
    
    long currentTimeMillis = ();
    String timeMillis = (currentTimeMillis);
    // File path    String base64key = Base64.encodeBase64String(());
    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 = ("upload-file", ".txt");
    // (tempFile, ());
    // ("file", new FileSystemResource(()));

	// =====================================================================================    // ① If the file exists, just get the file input stream directly	File file = new File("c:\\");
    ("file", new InputStreamResource((), originalFilename));

	// ② Temporary files, such as MultipartFile, get their byte streams to process	MultipartFile multipartFile = null;
	byte[] byteArray = ();
    ByteArrayResource byteArrayResource = new ByteArrayResource(byteArray) {
        @Override
        public String getFilename() {
        	// If the file name is not specified here, the file name uploaded will be garbled            return originalFilename;
        }
    };
    ("file", byteArrayResource);
	// ========================================================================

	// Other parameters are used as required    // key is file path    ("key", filePath);

    HttpHeaders headers = new HttpHeaders();
    (MediaType.MULTIPART_FORM_DATA);
    ("x-fs-timestamp", timeMillis);
    ("x-fs-appid", appId);
    ("x-fs-sign", sign);
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);

    RestTemplate restTemplate = ();
    (uploadUrl, requestEntity, );
}


/** Encryption **/
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);
}

download

Just note that the return value isbyte[]Just:

// resource is a unique identifier for the file, and it is used as requiredpublic static final byte[] dwAttach(String resource) {
    String appId = "";
    String accessKey = "";

    long currentTimeMillis = ();
    String timeMillis = (currentTimeMillis);
    String base64key = Base64.encodeBase64String(());
    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);
    ("appId", appId);
    ("timestamp", timeMillis);
    ("sign", sign);
    RestTemplate restTemplate = ();

    return (downloadUrl, byte[].class, uriVariables);
}

Can be passed in the futurenew ByteArrayInputStream(byte[])Convert to stream, or, and other tools, perform other processing.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.