shz.spring.DownloadHelp Maven / Gradle / Ivy
package shz.spring;
import shz.core.PRException;
import shz.core.io.IOHelp;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Objects;
public final class DownloadHelp {
private DownloadHelp() {
throw new IllegalStateException();
}
public static void download(HttpServletResponse response, InputStream is, long offset) {
Objects.requireNonNull(response);
Objects.requireNonNull(is);
try (OutputStream os = response.getOutputStream()) {
IOHelp.read(is, Math.max(0, offset), 0L, os, IOHelp.DEFAULT_DATA_SIZE, null, null);
} catch (IOException e) {
throw PRException.of(e);
}
}
public static void download(String contentType, String filename, InputStream is, long offset) {
Objects.requireNonNull(contentType);
Objects.requireNonNull(filename);
Objects.requireNonNull(is);
HttpServletResponse response = ServletHelp.getResponse();
Objects.requireNonNull(response);
try (OutputStream os = response.getOutputStream()) {
offset = Math.max(0, offset);
response.setContentLength(is.available() - (int) offset);
response.setContentType(contentType);
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
IOHelp.read(is, offset, 0L, os, IOHelp.DEFAULT_DATA_SIZE, null, null);
} catch (IOException e) {
throw PRException.of(e);
}
}
public static void download(HttpServletResponse response, FileInputStream fis, long offset) {
Objects.requireNonNull(response);
Objects.requireNonNull(fis);
try (OutputStream os = response.getOutputStream();
WritableByteChannel wbc = Channels.newChannel(os);
FileChannel fc = fis.getChannel()) {
offset = Math.max(0L, offset);
long size = fc.size() - offset;
if (size > 0L) {
fc.transferTo(offset, size, wbc);
os.flush();
}
} catch (IOException e) {
throw PRException.of(e);
}
}
public static void download(String contentType, String filename, FileInputStream fis, long offset) {
Objects.requireNonNull(contentType);
Objects.requireNonNull(filename);
Objects.requireNonNull(fis);
HttpServletResponse response = ServletHelp.getResponse();
Objects.requireNonNull(response);
try (OutputStream os = response.getOutputStream();
WritableByteChannel wbc = Channels.newChannel(os);
FileChannel fc = fis.getChannel()) {
offset = Math.max(0L, offset);
long size = fc.size() - offset;
if (size > 0L) {
response.setContentLength((int) size);
response.setContentType(contentType);
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
fc.transferTo(offset, size, wbc);
os.flush();
}
} catch (IOException e) {
throw PRException.of(e);
}
}
public static void download(String contentType, String filename, File file, long offset) {
try {
download(contentType, filename, new FileInputStream(file), offset);
} catch (FileNotFoundException e) {
throw PRException.of(e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy