io.github.imzix.commons.net.HttpClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons Show documentation
Show all versions of commons Show documentation
some commonly used secondary packaging tool classes
package io.github.imzix.commons.net;
import io.github.imzix.commons.file.ZipBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.function.Consumer;
@Slf4j
public class HttpClient {
private HttpClient() {
}
/**
* 导出zip文件
*
* @param fileName 文件名(不带后缀)
*/
public static ResponseEntity exportZip(String fileName, Consumer zipBuilderConsumer) {
log.info("导出压缩文件:{}", fileName);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipBuilder zipBuilder = ZipBuilder.getInstance(outputStream)) {
zipBuilderConsumer.accept(zipBuilder);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" +
URLEncoder.encode(fileName + ".zip", "UTF-8"))
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(outputStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=error.txt")
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body((e.getClass() + ":" + e.getMessage()).getBytes(StandardCharsets.UTF_8));
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy