工具类代码整理
Posted root_zhb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了工具类代码整理相关的知识,希望对你有一定的参考价值。
工具类代码整理
HttpClient
MultipartFile file = new MockMultipartFile("file", "hello-" + number + ".pdf", "application/pdf", pdfFile);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setContentType(ContentType.MULTIPART_FORM_DATA);
ContentType fileContentType = ContentType.create(ContentType.parse("application/pdf").getMimeType(), StandardCharsets.UTF_8);
builder.addBinaryBody("file",pdfFile,fileContentType, URLEncoder.encode(file.getOriginalFilename(), "utf-8"));
发送文件示例图
List分割工具类
- averageAssign 平均分割
- splitList 按照指定长度分割
public class ListSplitUtil
/**
* @param list
* @return
*/
public static <T> List<List<T>> averageAssign(List<T> list, int n)
List<List<T>> result=new ArrayList<List<T>>();
int remaider=list.size()%n; //(先计算出余数)
int number=list.size()/n; //然后是商
int offset=0;//偏移量
for(int i=0;i<n;i++)
List<T> value=null;
if(remaider>0)
value=list.subList(i*number+offset, (i+1)*number+offset+1);
remaider--;
offset++;
else
value=list.subList(i*number+offset, (i+1)*number+offset);
result.add(value);
return result;
/**
* @param list
* @param len
* @return
*/
public static<T> List<List<T>> splitList(List<T> list, int len)
if (list == null || list.size() == 0 || len < 1)
return null;
List<List<T>> result = new ArrayList<List<T>>();
int size = list.size();
int count = (size + len - 1) / len;
for (int i = 0; i < count; i++)
List<T> subList = list.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
result.add(subList);
return result;
图片转PDF,PDF转图片
图片转PDF
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.2</version>
</dependency>
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Image2PDF
/*** @param picturePath 图片地址*/
private static void createPic(Document document, String picturePath)
try
Image image = Image.getInstance(picturePath);
float documentWidth = document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin();
float documentHeight = documentWidth / 580 * 400;//重新设置宽高
image.scaleAbsolute(documentWidth, documentHeight);//重新设置宽高
document.add(image);
catch (Exception ex)
public static void image2pdf(String text, String pdf) throws DocumentException, IOException
Document document = new Document();
OutputStream os = new FileOutputStream(new File(pdf));
PdfWriter.getInstance(document,os);
document.open();
createPic(document,text);
document.close();
public static void main(String[] args) throws IOException, DocumentException
image2pdf("C:\\\\Users\\\\Administrator\\\\Desktop\\\\8.jpg","C:\\\\Users\\\\Administrator\\\\Desktop\\\\111.pdf");
PDF转图片
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.1</version>
</dependency>
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class PDFConvert
/**
* PDF转图片的流
*/
public static List<byte[]> PDF2JPG(InputStream inputStream) throws IOException
List<byte[]> imageData=new ArrayList<>();
OutputStream out = null;
if (inputStream==null)
throw new FileNotFoundException();
else
ByteArrayOutputStream bos = null;
BufferedInputStream in = null;
PDDocument doc = null;
try
in = new BufferedInputStream(inputStream);
doc = PDDocument.load(in);
PDFRenderer pdfRenderer = new PDFRenderer(doc);
PDPageTree pages = doc.getPages();
int pageCount = pages.getCount();
for (int i = 0; i < pageCount; i++)
BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 200);
bos = new ByteArrayOutputStream();
ImageIO.write(bim, "jpg", bos);
byte[] var7 = bos.toByteArray();
imageData.add(var7);
// //jpg文件转出路径
// out = new FileOutputStream("D:\\\\abc.jpg");
// out.write(bos.toByteArray());
return imageData;
finally
try
if (in != null)
in.close();
catch (IOException var14)
var14.printStackTrace();
bos.close();
以上是关于工具类代码整理的主要内容,如果未能解决你的问题,请参考以下文章