好使了, outputStream=new ByteArrayOutputStream();
// 从前端传入的MultipartFile加水印后返回新的MultipartFile用于后续业务逻辑处理
public MultipartFile DocWaterMark(MultipartFile file, String account, String suffix) {
File directory = new File(".");
final String originalFileName = file.getOriginalFilename();
String fileName = originalFileName;
String[] fileSplits = null;
String datapath = "", outputpath = "";
String wmfile = "test.docx";
Date date = new Date();
String dateStr = dateFormat.format(date);
OutputStream outputStream = null;
InputStream inputStream = null;
try {
Document document = new Document();
document.loadFromStream(file.getInputStream(), FileFormat.Docx);
// 写入水印
insertTextWatermark(document.getSections().get(0), account);
outputpath = directory.getCanonicalPath() + "/src/test/java/output/" + wmfile;
File testfile = new File(outputpath);
outputStream=new ByteArrayOutputStream();
document.saveToStream(outputStream, FileFormat.Docx);
// 从输出流转换到输入流,scu为将outputStream和inputStream互转的工具类StreamConvertUtil
inputStream = scu.parse(outputStream);
file = getMultipartFile(inputStream, fileName);
// 写入指定的目录用于测试
file.transferTo(testfile);
// 返回到业务逻辑调用用于后续处理
return file;
} catch (Exception e) {
System.out.println(e.toString());
}
System.out.println("文件名:" + fileName + " 文件类型:" + file.getContentType() + " 文件大小:" + file.getSize() + "增加 " + suffix + "水印 " + " 责任人:" + account + " 时间:" + dateStr);
return null;
}
@Component
public class StreamConvertUtil {
//inputStream转outputStream
public ByteArrayOutputStream parse(InputStream in) throws Exception {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
int ch;
while ((ch = in.read()) != -1) {
swapStream.write(ch);
}
return swapStream;
}
//outputStream转inputStream
public ByteArrayInputStream parse(OutputStream out) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos = (ByteArrayOutputStream) out;
ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
return swapStream;
}
//inputStream转String
public String parse_String(InputStream in) throws Exception {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
int ch;
while ((ch = in.read()) != -1) {
swapStream.write(ch);
}
return swapStream.toString();
}
//OutputStream 转String
public String parse_String(OutputStream out) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos = (ByteArrayOutputStream) out;
ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
return swapStream.toString();
}
//String转inputStream
public ByteArrayInputStream parse_inputStream(String in) throws Exception {
ByteArrayInputStream input = new ByteArrayInputStream(in.getBytes());
return input;
}
//String 转outputStream
public ByteArrayOutputStream parse_outputStream(String in) throws Exception {
return parse(parse_inputStream(in));
}
}
/**
* 获取封装得MultipartFile
*
* @param inputStream inputStream
* @param fileName fileName
* @return MultipartFile
*/
public MultipartFile getMultipartFile(InputStream inputStream, String fileName) {
FileItem fileItem = createFileItem(inputStream, fileName);
//CommonsMultipartFile是feign对multipartFile的封装,但是要FileItem类对象
return new CommonsMultipartFile(fileItem);
}
/**
* FileItem类对象创建
*
* @param inputStream inputStream
* @param fileName fileName
* @return FileItem
*/
public FileItem createFileItem(InputStream inputStream, String fileName) {
FileItemFactory factory = new DiskFileItemFactory(16, null);
String textFieldName = "file";
FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
OutputStream os = null;
//使用输出流输出输入流的字节
try {
os = item.getOutputStream();
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
inputStream.close();
} catch (IOException e) {
logger.error("Stream copy exception", e);
throw new IllegalArgumentException("文件上传失败");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
logger.error("Stream close exception", e);
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
logger.error("Stream close exception", e);
}
}
}
return item;
}
}