Commit f9f6fa73 authored by 孙杰's avatar 孙杰

项目pdf

parent bb02446d
...@@ -85,6 +85,73 @@ ...@@ -85,6 +85,73 @@
<artifactId>spring-boot-starter-amqp</artifactId> <artifactId>spring-boot-starter-amqp</artifactId>
</dependency> </dependency>
<!--pdf -->
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!--条形码 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!-- 谷歌二维码 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.1.0</version>
</dependency>
<!-- 阿里云上传 -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
......
package org.ta.pddserver.PDFTemplate;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
/**
*
*/
public class BarCodeUtils {
/** 条形码宽度 */
private static final int WIDTH = 500;
/** 条形码高度 */
private static final int HEIGHT = 70;
/** 加文字 条形码 */
private static final int WORDHEIGHT = 95;
/**
* 设置 条形码参数
*/
private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
private static final long serialVersionUID = 1L;
{
// 设置编码方式
put(EncodeHintType.CHARACTER_SET, "utf-8");
}
};
/**
* 把带logo的二维码下面加上文字
* @author fxbin
* @param image 条形码图片
* @param words 文字
* @return 返回BufferedImage
*/
public static BufferedImage insertWords(BufferedImage image, String words){
// 新的图片,把带logo的二维码下面加上文字
if (StringUtils.isNotEmpty(words)) {
BufferedImage outImage = new BufferedImage(WIDTH, WORDHEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = outImage.createGraphics();
// 抗锯齿
setGraphics2D(g2d);
// 设置白色
setColorWhite(g2d);
// 画条形码到新的面板
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
// 画文字到新的面板
Color color=new Color(0, 0, 0);
g2d.setColor(color);
// 字体、字型、字号
g2d.setFont(new Font("微软雅黑", Font.PLAIN, 18));
//文字长度
int strWidth = g2d.getFontMetrics().stringWidth(words);
//总长度减去文字长度的一半 (居中显示)
int wordStartX=(WIDTH - strWidth) / 2;
//height + (outImage.getHeight() - height) / 2 + 12
int wordStartY=HEIGHT+20;
// 画文字
g2d.drawString(words, wordStartX, wordStartY);
g2d.dispose();
outImage.flush();
return outImage;
}
return null;
}
/**
* 生成 图片缓冲
* @author fxbin
* @param vaNumber VA 码
* @return 返回BufferedImage
*/
public static BufferedImage getBarCode(String vaNumber){
try {
Code128Writer writer = new Code128Writer();
// 编码内容, 编码类型, 宽度, 高度, 设置参数
BitMatrix bitMatrix = writer.encode(vaNumber, BarcodeFormat.CODE_128, WIDTH, HEIGHT, hints);
return MatrixToImageWriter.toBufferedImage(bitMatrix);
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
/**
* 生成 图片缓冲图片大小自己输入
* @author fxbin
* @param vaNumber VA 码
* @return 返回BufferedImage
*/
public static BufferedImage getBarCode(String vaNumber,int width,int height){
try {
Code128Writer writer = new Code128Writer();
// 编码内容, 编码类型, 宽度, 高度, 设置参数
BitMatrix bitMatrix = writer.encode(vaNumber, BarcodeFormat.CODE_128, width, height, hints);
return MatrixToImageWriter.toBufferedImage(bitMatrix);
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
/**
* 把带logo的二维码下面加上文字大小自己输入
* @author fxbin
* @param image 条形码图片
* @param words 文字
* @return 返回BufferedImage
*/
public static BufferedImage insertWords(BufferedImage image, String words ,int width,int wordheight,int height){
// 新的图片,把带logo的二维码下面加上文字
if (StringUtils.isNotEmpty(words)) {
BufferedImage outImage = new BufferedImage(width, wordheight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = outImage.createGraphics();
// 抗锯齿
setGraphics2D(g2d);
// 设置白色
setColorWhite(g2d);
// 画条形码到新的面板
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
// 画文字到新的面板
Color color=new Color(0, 0, 0);
g2d.setColor(color);
// 字体、字型、字号
g2d.setFont(new Font("微软雅黑", Font.PLAIN, 18));
//文字长度
int strWidth = g2d.getFontMetrics().stringWidth(words);
//总长度减去文字长度的一半 (居中显示)
int wordStartX=(width - strWidth) / 2;
//height + (outImage.getHeight() - height) / 2 + 12
int wordStartY=height+20;
// 画文字
g2d.drawString(words, wordStartX, wordStartY);
g2d.dispose();
outImage.flush();
return outImage;
}
return null;
}
/**
* 设置 Graphics2D 属性 (抗锯齿)
* @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制
*/
private static void setGraphics2D(Graphics2D g2d){
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
Stroke s = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
g2d.setStroke(s);
}
/**
* 设置背景为白色
* @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制
*/
private static void setColorWhite(Graphics2D g2d){
g2d.setColor(Color.WHITE);
//填充整个屏幕
g2d.fillRect(0,0,600,600);
//设置笔刷
g2d.setColor(Color.BLACK);
}
}
package org.ta.pddserver.PDFTemplate;
import java.io.*;
/**
* 文件操作代码
*
* @author cn.outofmemory
* @date 2013-1-7
*/
public class FileUtils {
/**
* 将文本文件中的内容读入到buffer中
* @param buffer buffer
* @param filePath 文件路径
* @throws IOException 异常
* @author cn.outofmemory
* @date 2013-1-7
*/
public static void readToBuffer(StringBuffer buffer, String filePath) throws IOException {
InputStream is = new FileInputStream(filePath);
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\n"); // 添加换行符
line = reader.readLine(); // 读取下一行
}
reader.close();
is.close();
}
/**
* 读取文本文件内容
* @param filePath 文件所在路径
* @return 文本内容
* @throws IOException 异常
* @author cn.outofmemory
* @date 2013-1-7
*/
public static String readFile(String filePath){
try {
StringBuffer sb = new StringBuffer();
FileUtils.readToBuffer(sb, filePath);
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public static long getFileSize(String filename) {
File file = new File(filename);
if (!file.exists() || !file.isFile()) {
System.out.println("文件不存在");
return -1;
}
return file.length();
}
public static long getFileSize(File file) {
if (!file.exists() || !file.isFile()) {
System.out.println("文件不存在");
return -1;
}
return file.length();
}
/*
public static String genChecksum(String file, CheckSumAlgoType checkSumAlgoType) {
*/
/**
* 使用org.apache.commons.codec.digest.DigestUtils
*//*
String checksum = null;
try {
switch (checkSumAlgoType) {
case MD5:
checksum = DigestUtils.md5Hex(new FileInputStream(file));
break;
case SHA_1:
checksum = DigestUtils.sha1Hex(new FileInputStream(file));
break;
case SHA_256:
checksum = DigestUtils.sha256Hex(new FileInputStream(file));
break;
case SHA_512:
checksum = DigestUtils.sha512Hex(new FileInputStream(file));
break;
default:
checksum = DigestUtils.md5Hex(new FileInputStream(file));
}
} catch (Exception e) {
e.printStackTrace();
}
return checksum;
}
public static String genChecksum(File file, CheckSumAlgoType checkSumAlgoType) {
*/
/**
* 使用org.apache.commons.codec.digest.DigestUtils
*//*
String checksum = null;
try {
switch (checkSumAlgoType) {
case MD5:
checksum = DigestUtils.md5Hex(new FileInputStream(file));
break;
case SHA_1:
checksum = DigestUtils.sha1Hex(new FileInputStream(file));
break;
case SHA_256:
checksum = DigestUtils.sha256Hex(new FileInputStream(file));
break;
case SHA_512:
checksum = DigestUtils.sha512Hex(new FileInputStream(file));
break;
default:
checksum = DigestUtils.md5Hex(new FileInputStream(file));
}
} catch (Exception e) {
e.printStackTrace();
}
return checksum;
}
*/
public static void copy(String source, String dest, int bufferSize) {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(source);
out = new FileOutputStream(dest);
byte[] buffer = new byte[bufferSize];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean checkDir(String dirName) {
try {
File dir = new File(dirName);
if (dir.exists()) {
System.out.println("创建目录" + dirName + "失败,目标目录已经存在");
} else {
if (!dirName.endsWith(File.separator)) {
dirName = dirName + File.separator;
}
if (dir.mkdirs()) {
System.out.println("创建目录" + dirName + "成功!");
} else {
System.out.println("创建目录" + dirName + "失败!");
return false;
}
}
}catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
//删除文件夹
//param folderPath 文件夹完整绝对路径
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
myFilePath.delete(); //删除空文件夹
} catch (Exception e) {
e.printStackTrace();
}
}
//删除指定文件夹下所有文件
//param path 文件夹完整绝对路径
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);//再删除空文件夹
flag = true;
}
}
return flag;
}
}
\ No newline at end of file
package org.ta.pddserver.PDFTemplate;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Random;
public class QRCodeUtils {
private static final String CHARSET = "utf-8";
private static final String FORMAT = "JPG";
// 二维码尺寸
private static final int QRCODE_SIZE = 3000;
// LOGO宽度
private static final int LOGO_WIDTH = 600;
// LOGO高度
private static final int LOGO_HEIGHT = 600;
static BufferedImage createImage(String content, String logoPath, boolean needCompress) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
if (logoPath == null || "".equals(logoPath)) {
return image;
}
// 插入图片
QRCodeUtils.insertImage(image, logoPath, needCompress);
return image;
}
/**
* 插入LOGO
*
* @param source 二维码图片
* @param logoPath LOGO图片地址
* @param needCompress 是否压缩
* @throws Exception
*/
private static void insertImage(BufferedImage source, String logoPath, boolean needCompress) throws Exception {
File file = new File(logoPath);
if (!file.exists()) {
throw new Exception("logo file not found.");
}
Image src = ImageIO.read(new File(logoPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { // 压缩LOGO
if (width > LOGO_WIDTH) {
width = LOGO_WIDTH;
}
if (height > LOGO_HEIGHT) {
height = LOGO_HEIGHT;
}
Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
/**
* 生成二维码(内嵌LOGO)
* 二维码文件名随机,文件名可能会有重复
*
* @param content 内容
* @param logoPath LOGO地址
* @param destPath 存放目录
* @param needCompress 是否压缩LOGO
* @throws Exception
*/
public static String encode(String content, String logoPath, String destPath, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtils.createImage(content, logoPath, needCompress);
mkdirs(destPath);
String fileName = new Random().nextInt(99999999) + "." + FORMAT.toLowerCase();
ImageIO.write(image, FORMAT, new File(destPath + "/" + fileName));
return fileName;
}
/**
* 生成二维码(内嵌LOGO)
* 调用者指定二维码文件名
*
* @param content 内容
* @param logoPath LOGO地址
* @param destPath 存放目录
* @param fileName 二维码文件名
* @param needCompress 是否压缩LOGO
* @throws Exception
*/
public static String encode(String content, String logoPath, String destPath, String fileName, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtils.createImage(content, logoPath, needCompress);
mkdirs(destPath);
fileName = fileName.substring(0, fileName.indexOf(".") > 0 ? fileName.indexOf(".") : fileName.length())
+ "." + FORMAT.toLowerCase();
ImageIO.write(image, FORMAT, new File(destPath + "/" + fileName));
return fileName;
}
/**
* 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.
* (mkdir如果父目录不存在则会抛出异常)
*
* @param destPath 存放目录
*/
public static void mkdirs(String destPath) {
File file = new File(destPath);
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
}
/**
* 生成二维码(内嵌LOGO)
*
* @param content 内容
* @param logoPath LOGO地址
* @param destPath 存储地址
* @throws Exception
*/
public static String encode(String content, String logoPath, String destPath) throws Exception {
return QRCodeUtils.encode(content, logoPath, destPath, false);
}
/**
* 生成二维码
*
* @param content 内容
* @param destPath 存储地址
* @param needCompress 是否压缩LOGO
* @throws Exception
*/
public static String encode(String content, String destPath, boolean needCompress) throws Exception {
return QRCodeUtils.encode(content, null, destPath, needCompress);
}
/**
* 生成二维码
*
* @param content 内容
* @param destPath 存储地址
* @throws Exception
*/
public static String encode(String content, String destPath) throws Exception {
return QRCodeUtils.encode(content, null, destPath, false);
}
/**
* 生成二维码(内嵌LOGO)
*
* @param content 内容
* @param logoPath LOGO地址
* @param output 输出流
* @param needCompress 是否压缩LOGO
* @throws Exception
*/
public static void encode(String content, String logoPath, OutputStream output, boolean needCompress)
throws Exception {
BufferedImage image = QRCodeUtils.createImage(content, logoPath, needCompress);
ImageIO.write(image, FORMAT, output);
}
/**
* 生成二维码
*
* @param content 内容
* @param output 输出流
* @throws Exception
*/
public static void encode(String content, OutputStream output) throws Exception {
QRCodeUtils.encode(content, null, output, false);
}
/**
* 解析二维码
*
* @param file 二维码图片
* @return
* @throws Exception
*/
public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}
/**
* 解析二维码
*
* @param path 二维码图片地址
* @return
* @throws Exception
*/
public static String decode(String path) throws Exception {
return QRCodeUtils.decode(new File(path));
}
public static void main(String[] args) throws Exception {
String text = "http://www.baidu.com";
//不含Logo
// QRCodeUtils.encode(text, null, "e:\\", true);
String tex1 = "www.baidu.com";
//不含Logo
QRCodeUtils.encode(tex1, null, "e:\\", true);
//含Logo,不指定二维码图片名
//QRCodeUtils.encode(text, "E:\\666.jpg", "E:\\", true);
//含Logo,指定二维码图片名
//QRCodeUtils.encode(text, "E:\\logo.png", "E:\\", "qrcode1", true);
//String decode = QRCodeUtil.decode("e:\\qrcode.jpg");
//System.out.println(decode);
}
}
package org.ta.pddserver.PDFTemplate;
import jakarta.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
public boolean makeZip(String pdfName,Map<String, ByteArrayOutputStream> outMap, String name, HttpServletResponse response) throws IOException {
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("fileName", name);
ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
try {
byte[] buf = new byte[1024];
for (String key : outMap.keySet()) {
out.putNextEntry(new ZipEntry(pdfName+"-" + key + ".pdf"));
byte[] data = outMap.get(key).toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(data);
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
response.flushBuffer();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
out.close();
}
return false;
}
}
package org.ta.pddserver.cos;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
public class Uploader {
private static final String OSS_END_POINT = "oss-cn-qingdao.aliyuncs.com";
private static final String OSS_ACCESS_KEY_ID = "LTAI5t8BWUq8KYFPaE6qoAt6";
private static final String OSS_ACCESS_KEY_SECRET = "WxtupLLdjZLPBwtJ8SL91W5IYVGBdF";
private static final String OSS_BUCKET_NAME = "tradeany-server";
public String uploadOssFile(MultipartFile file) {
String endpoint = OSS_END_POINT;
String accessKeyId = OSS_ACCESS_KEY_ID;
String accessKeySecret = OSS_ACCESS_KEY_SECRET;
String bucketName = OSS_BUCKET_NAME;
String url = null;
try {
//创建OSSClient实例。
OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//获取上传文件输入流
InputStream inputStream = file.getInputStream();
//获取文件名称
String fileName = file.getOriginalFilename();
//保证文件名唯一,去掉uuid中的'-'
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
fileName = uuid + fileName;
//把文件按日期分类,构建日期路径:avatar/2019/02/26/文件名
//String datePath = new Date().toString("yyyy/MM/dd");
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String time = formatter.format(new Date());
String eName = fileName.substring(fileName.lastIndexOf("."));
//拼接
fileName = "files/" + time + "/" + uuid + eName;
//调用oss方法上传到阿里云
//第一个参数:Bucket名称
//第二个参数:上传到oss文件路径和文件名称
//第三个参数:上传文件输入流
ossClient.putObject(bucketName, fileName, inputStream);
//把上传后把文件url返回
//https://xppll.oss-cn-beijing.aliyuncs.com/01.jpg
url = "https://images.v2.tradeany.com/" + fileName;
System.out.println("file:" + url);
//关闭OSSClient
ossClient.shutdown();
return url;
} catch (Exception e) {
e.printStackTrace();
}
return url;
}
public static String uploadOssInputStream(InputStream inputStream, String fileName) {
String endpoint = OSS_END_POINT;
String accessKeyId = OSS_ACCESS_KEY_ID;
String accessKeySecret = OSS_ACCESS_KEY_SECRET;
String bucketName = OSS_BUCKET_NAME;
String url = null;
try {
//创建OSSClient实例。
OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//保证文件名唯一,去掉uuid中的'-'
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
fileName = uuid + fileName;
//把文件按日期分类,构建日期路径:avatar/2019/02/26/文件名
//String datePath = new Date().toString("yyyy/MM/dd");
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String time = formatter.format(new Date());
String eName = fileName.substring(fileName.lastIndexOf("."));
//拼接
fileName = "files/" + time + "/" + uuid + eName;
//调用oss方法上传到阿里云
//第一个参数:Bucket名称
//第二个参数:上传到oss文件路径和文件名称
//第三个参数:上传文件输入流
ossClient.putObject(bucketName, fileName, inputStream);
//把上传后把文件url返回
//https://xppll.oss-cn-beijing.aliyuncs.com/01.jpg
url = "https://images.v2.tradeany.com/" + fileName;
System.out.println("file:" + url);
//关闭OSSClient
ossClient.shutdown();
return url;
} catch (Exception e) {
e.printStackTrace();
}
return url;
}
/**
* 上传到阿里云
*
* @param stream 文件内容
* @param filename 带路径文件名 postFiles/111.abc
* @return 链接
*/
public static String uploadOssStream(ByteArrayOutputStream stream, String filename) {
String endpoint = OSS_END_POINT;
String accessKeyId = OSS_ACCESS_KEY_ID;
String accessKeySecret = OSS_ACCESS_KEY_SECRET;
String bucketName = OSS_BUCKET_NAME;
String url = null;
try {
//创建OSSClient实例。
OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//保证文件名唯一,去掉uuid中的'-'
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
filename = "pdfFiles/" +uuid +filename;
//获取上传文件输入流
ByteArrayInputStream inputStream = null;
try {
byte[] data = stream.toByteArray();
inputStream = new ByteArrayInputStream(data);
} catch (Exception e) {
e.printStackTrace();
}
//调用oss方法上传到阿里云
//第一个参数:Bucket名称
//第二个参数:上传到oss文件路径和文件名称
//第三个参数:上传文件输入流
ossClient.putObject(bucketName, filename, inputStream);
//把上传后把文件url返回
//https://xppll.oss-cn-beijing.aliyuncs.com/01.jpg
url = "https://images.v2.tradeany.com/" + filename;
System.out.println("file:" + url);
//关闭OSSClient
ossClient.shutdown();
return url;
} catch (Exception e) {
e.printStackTrace();
}
return url;
}
}
package org.ta.pddserver.model.to;
import lombok.Data;
@Data
public class PrintOrderInfoTO {
private String uid;
private String expressTag;
private String expressUid;
private String landingUid;
private String addressUpperLeft;
private String addressUpperRight;
private String addressBottomLeft;
private String addressBottomRight;
private String recipient;
private String detailedAddress;
private String orderCore;
//暂时不用
private String pageNo;
private String feeTag;
private String goodNameTag;
private String goodName;
private String printTime;
}
...@@ -20,4 +20,10 @@ public class TimeTool { ...@@ -20,4 +20,10 @@ public class TimeTool {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date); return sdf.format(date);
} }
public String getNowDateString() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
return sdf.format(date);
}
} }
\ No newline at end of file
lotte_T.png

1.16 KB

Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment