test
This commit is contained in:
@@ -9,6 +9,7 @@ import com.point.strategy.user.bean.User;
|
||||
import com.point.strategy.user.service.UserService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.imaging.ImageInfo;
|
||||
import org.apache.commons.imaging.Imaging;
|
||||
import org.jaudiotagger.audio.AudioFile;
|
||||
@@ -41,6 +42,7 @@ import java.util.Map;
|
||||
|
||||
import static com.point.strategy.common.WatermarkImgUtils.*;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/v/archiveFile")
|
||||
@Api(tags = "原文管理", value = "ArchiveFileController")
|
||||
@@ -88,13 +90,24 @@ public class ArchiveFileController {
|
||||
ServletOutputStream out = null;
|
||||
String[] split = fileName.split("\\.");
|
||||
ByteArrayOutputStream outputStream = null;
|
||||
|
||||
// 先解码路径和文件名
|
||||
try {
|
||||
path = URLDecoder.decode(path, "UTF-8");
|
||||
fileName = URLDecoder.decode(fileName, "UTF-8");
|
||||
} catch (Exception e) {
|
||||
log.error("URL解码失败: {}", e.getMessage());
|
||||
}
|
||||
|
||||
if(!split[split.length-1].equalsIgnoreCase("jpg")&&!split[split.length-1].equalsIgnoreCase("png")&&!split[split.length-1].equalsIgnoreCase("pdf")){
|
||||
response.reset();
|
||||
response.setContentType( "application/octet-stream" );
|
||||
response.addHeader( "content-disposition", "attachment; filename=" + fileName);
|
||||
// 设置正确的Content-Type和编码
|
||||
response.setContentType("application/octet-stream;charset=UTF-8");
|
||||
// 对中文文件名进行URL编码
|
||||
String encodedFileName = java.net.URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
|
||||
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName);
|
||||
|
||||
try {
|
||||
path = URLDecoder.decode(path,"UTF-8");
|
||||
fileName = URLDecoder.decode(fileName,"UTF-8");
|
||||
// String dir = uploadPath + File.separator + path;
|
||||
String downLoadPath = path + File.separator + fileName;
|
||||
in = new FileInputStream(downLoadPath);
|
||||
@@ -106,49 +119,50 @@ public class ArchiveFileController {
|
||||
}
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
log.error("文件下载失败: {}", e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
try {
|
||||
in.close();
|
||||
out.close();
|
||||
if (in != null) in.close();
|
||||
if (out != null) out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
log.error("关闭流失败: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
}else { //需要添加水印展示的文件
|
||||
path = URLDecoder.decode(path, "UTF-8");
|
||||
fileName = URLDecoder.decode(fileName, "UTF-8");
|
||||
// String dir = uploadPath + File.separator + path;
|
||||
String downLoadPath = path + File.separator + fileName;
|
||||
try {
|
||||
if (split[split.length-1].equalsIgnoreCase("pdf")){
|
||||
|
||||
in = new FileInputStream(downLoadPath);
|
||||
outputStream = PdfFileHelper.waterMark(in,user.getUsername());
|
||||
// 设置PDF的Content-Type
|
||||
response.setContentType("application/pdf;charset=UTF-8");
|
||||
out = response.getOutputStream();
|
||||
out.write(outputStream.toByteArray());
|
||||
out.flush();
|
||||
}else {
|
||||
outputStream = addWatermarkByFileIo(downLoadPath, user.getUsername());
|
||||
// 根据文件类型设置正确的Content-Type
|
||||
String contentType = "image/jpeg";
|
||||
if (split[split.length-1].equalsIgnoreCase("png")) {
|
||||
contentType = "image/png";
|
||||
}
|
||||
response.setContentType(contentType + ";charset=UTF-8");
|
||||
out = response.getOutputStream();
|
||||
out.write(outputStream.toByteArray());
|
||||
out.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
log.error("水印处理失败: {}", e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
try {
|
||||
if (in!=null){
|
||||
in.close();
|
||||
}
|
||||
if (out!=null){
|
||||
out.close();
|
||||
}
|
||||
if (outputStream!=null){
|
||||
outputStream.close();
|
||||
}
|
||||
if (in!=null) in.close();
|
||||
if (out!=null) out.close();
|
||||
if (outputStream!=null) outputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
log.error("关闭流失败: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,8 +24,47 @@ public class WatermarkImgUtils {
|
||||
|
||||
//水印颜色
|
||||
private static Color markContentColor = Color.lightGray;
|
||||
//水印字体,大小
|
||||
private static Font font = new Font("宋体", Font.BOLD, 24);
|
||||
//水印字体,大小 - 使用支持中文的字体
|
||||
private static Font font = createChineseFont();
|
||||
|
||||
/**
|
||||
* 创建支持中文的字体
|
||||
* @return 支持中文的Font对象
|
||||
*/
|
||||
private static Font createChineseFont() {
|
||||
try {
|
||||
// 尝试多种中文字体
|
||||
String[] chineseFonts = {"SimSun", "宋体", "SimHei", "黑体", "Microsoft YaHei", "微软雅黑", "Arial Unicode MS", "Dialog"};
|
||||
|
||||
for (String fontName : chineseFonts) {
|
||||
Font font = new Font(fontName, Font.BOLD, 24);
|
||||
// 测试字体是否支持中文
|
||||
if (canDisplayChinese(font)) {
|
||||
System.out.println("使用字体: " + fontName);
|
||||
return font;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果都不支持,使用默认字体但降低要求
|
||||
System.out.println("使用默认字体,可能无法显示中文");
|
||||
return new Font("Dialog", Font.BOLD, 24);
|
||||
} catch (Exception e) {
|
||||
System.err.println("字体创建失败,使用默认字体: " + e.getMessage());
|
||||
return new Font("Dialog", Font.BOLD, 24);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字体是否支持中文显示
|
||||
*/
|
||||
private static boolean canDisplayChinese(Font font) {
|
||||
try {
|
||||
// 使用简单的中文字符测试
|
||||
return font.canDisplay('中') && font.canDisplay('文');
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//设置水印文字的旋转角度
|
||||
private static Integer degree = 45;
|
||||
//设置水印透明度
|
||||
|
||||
Reference in New Issue
Block a user