文件(通过链接)下载工具类
package com.loongxi.eiss.modules.produce.proutils;
import com.loongxi.consumer.bean.DownloadInvokePacket;
import com.loongxi.eiss.modules.common.enums.SystemEnum;
import com.loongxi.eiss.utils.InvokePacketUtil;
import com.loongxi.eiss.utils.MinioUtil;
import org.springblade.core.tool.utils.DateUtil;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class GetFileUtil {
public static void getByMinIoUrl(String url, HttpServletResponse response) {
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
String[] split = url.split("\\.");
String suffix = split[1];
DownloadInvokePacket packet = InvokePacketUtil.getDownloadInvokePacketForRandomUrl("综合检索文件下载", "132", "userName", suffix, SystemEnum.MANAGE, SystemEnum.MINIO);
File file = MinioUtil.publishDownload(packet, url);
response.setContentType("application/octet-stream");
response.setHeader("content-type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName=download" + DateUtil.time());
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
}catch (Exception e) {
System.out.println(1);
}finally {
if (bis != null) {
try {
bis.close();
} catch (IOException ignored) {
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException ignored) {
}
}
}
}
public static File getByUrl(String urlString,String suffix) {
InputStream is = null;
FileOutputStream os = null;
try {
URL url = new URL(urlString);
URLConnection con = url.openConnection();
is = con.getInputStream();
byte[] bs = new byte[1024];
int len;
String randomTmpFilePath = getRandomTmpFilePath(suffix);
File file = new File(randomTmpFilePath);
os = new FileOutputStream(file, true);
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
return file;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static String getRandomTmpFilePath(String suffix){
return DownloadInvokePacket.getRandomTmpFilePath(suffix);
}
}
java打印工具类(使用api调用打印机,适用win和linux)
package com.loongxi.eiss.utils;
import com.loongxi.consumer.bean.DownloadInvokePacket;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPrintable;
import org.apache.pdfbox.printing.Scaling;
import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Destination;
import javax.print.attribute.standard.Sides;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.PrinterJob;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
public class PrintUtil {
public static void PrintFile(File file,String printerNames) throws Exception {
String suffix = file.getName().split("\\.")[1];
String path = file.getPath();
String[] printerArr= printerNames.split(",");
for (String printerName : printerArr) {
switch (suffix){
case "png":
printImageToPdf(path,printerName,"png");
break;
case "jpeg":
printImageToPdf(path,printerName,"jpeg");
break;
case "pdf":
printPDF(path,printerName);
break;
case "docx":
printWord(path,printerName);
break;
case "doc":
printWord(path,printerName);
break;
default:
throw new Exception("不支持的打印类型!");
}
}
}
public static void printText(String txtFilePath,String printerName) throws Exception{
File file = new File(txtFilePath);
if (file == null) {
System.err.println("缺少打印文件");
}
InputStream fis = null;
try {
PrintService printService = getPrinter(printerName);
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
PrintRequestAttributeSet option = new HashPrintRequestAttributeSet();
option.add(new Copies(1));
option.add(Sides.DUPLEX);
byte[] fileBytes;
fis = new FileInputStream(txtFilePath);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int bytesRead;
byte[] buffer = new byte[4096];
while ((bytesRead = fis.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
fileBytes = outputStream.toByteArray();
Doc doc = new SimpleDoc(fileBytes, flavor, null);
DocPrintJob job = printService.createPrintJob();
job.print(doc, option);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("打印文件出错:" + e.getMessage());
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void printWord(String wordFilePath,String printerName) throws Exception{
try {
String pdfPath = changeWordToPDF(wordFilePath);
printPDF(pdfPath,printerName);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("打印文件出错:" + e.getMessage());
}
}
public static void printPDF(String filePath,String printerName) throws Exception {
File file = new File(filePath);
PDDocument document = null;
try {
document = PDDocument.load(file);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setJobName("打印" + file.getName());
PrintService printService = getPrinter(printerName);
printJob.setPrintService(printService);
Book book = PDFBookOption(document);
printJob.setPageable(book);
printJob.setCopies(1);
PrintRequestAttributeSet option = getAttributeSetForPDF();
printJob.print(option);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("打印文件出错:" + e.getMessage());
} finally {
if (document != null) {
try {
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void printImageToPdf(String filePath,String printerName,String suffix) throws Exception {
File file = new File(filePath);
if (file == null) {
System.err.println("缺少打印文件");
}
InputStream fis = null;
try {
DocFlavor flavor = getImageDocFlavor(suffix);
PrintRequestAttributeSet aset = getAttributeSetForImage(suffix);
PrintService printService = getPrinter(printerName);
fis = new FileInputStream(file);
Doc doc = new SimpleDoc(fis, flavor, null);
DocPrintJob job = printService.createPrintJob();
job.print(doc, aset);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("打印文件出错:" + e.getMessage());
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static ArrayList<String> getAllPrinter() throws Exception {
PrintService printService = null;
PrintService[] printServices = PrinterJob.lookupPrintServices();
ArrayList<String> names = new ArrayList<String>();
for (PrintService printer : printServices){
String name = printer.getName();
names.add(name);
}
return names;
}
private static PrintRequestAttributeSet getAttributeSetForPDF() throws URISyntaxException {
PrintRequestAttributeSet option = new HashPrintRequestAttributeSet();
option.add(Sides.DUPLEX);
URI outputUri = getPDFPrinterRandomTmpFilePath("pdf");
return option;
}
private static Book PDFBookOption(PDDocument document) throws Exception {
if(document == null){
throw new Exception("文档不存在!");
}
PDFPrintable pdfPrintable = new PDFPrintable(document, Scaling.ACTUAL_SIZE);
Book book = new Book();
PageFormat pageFormat = new PageFormat();
pageFormat.setOrientation(PageFormat.PORTRAIT);
book.append(pdfPrintable, pageFormat, document.getNumberOfPages());
return book;
}
private static PrintRequestAttributeSet getAttributeSetForImage(String suffix) throws URISyntaxException {
PrintRequestAttributeSet option = new HashPrintRequestAttributeSet();
option.add(new Copies(1));
option.add(Sides.DUPLEX);
URI outputUri = getPDFPrinterRandomTmpFilePath(suffix);
option.add(new Destination(outputUri));
return option;
}
private static DocFlavor getImageDocFlavor(String suffix) throws Exception {
DocFlavor flavor = null;
switch (suffix){
case "png":
flavor = DocFlavor.INPUT_STREAM.PNG;
break;
case "jpeg":
flavor = DocFlavor.INPUT_STREAM.JPEG;
break;
default:
throw new Exception("不支持的图片类型!");
}
return flavor;
}
private static PrintService getPrinter(String printerName) throws Exception {
PrintService printService = null;
if (printerName != null) {
PrintService[] printServices = PrinterJob.lookupPrintServices();
if (printServices == null || printServices.length == 0) {
throw new Exception("打印失败!未找到可用打印机");
}
for (int i = 0; i < printServices.length; i++) {
if (printServices[i].getName().contains(printerName)) {
printService = printServices[i];
System.out.println("找到打印机:" + printService.getName());
System.out.println("打印机 描述: " + printService.toString());
break;
}
}
if (printService == null) {
throw new Exception("打印失败!" + "未找到名称为" + printerName + "的打印机!");
}
}else{
throw new Exception("打印失败:" + "未选择打印机!");
}
return printService;
}
private static String getRandomTmpFilePath(String suffix){
return DownloadInvokePacket.getRandomTmpFilePath(suffix);
}
public static URI getPDFPrinterRandomTmpFilePath(String suffix) throws URISyntaxException {
String cacheFolderPath = System.getProperty("java.io.tmpdir");
String outputFileName = + LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")) +".pdf";
String outputPath = "file:\\" + cacheFolderPath + outputFileName;
URI outputUri = new URI(outputPath.replace("\\","/"));
return outputUri;
}
private static String changeWordToPDF(String filePath) throws Exception{
String pdfPath = getRandomTmpFilePath("pdf");
AsposeUtil.wordToPdf(filePath,pdfPath,null);
return pdfPath;
}
}