Skip to content

Commit ca445fd

Browse files
committed
feat: 重构方法,增加输入检测
1 parent a313062 commit ca445fd

15 files changed

Lines changed: 86 additions & 44 deletions

src/main/java/Main.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1+
import util.ConvertUtil;
12
import util.DebugUtil;
23
import util.FileCheckUtil;
3-
import util.ConvertUtil;
44

55
import java.nio.file.Files;
66
import java.nio.file.Paths;
7+
import java.util.HashSet;
78
import java.util.Scanner;
89

910
/**
1011
* @author weloe
1112
*/
1213
public class Main {
1314

15+
static HashSet<String> convertMethodSet = new HashSet<>();
16+
static {
17+
convertMethodSet.add("pdf2word");
18+
convertMethodSet.add("pdf2image");
19+
convertMethodSet.add("word2html");
20+
convertMethodSet.add("word2image");
21+
convertMethodSet.add("word2pdf");
22+
}
23+
1424
public static void main(String[] args) {
1525
Scanner scan = new Scanner(System.in);
1626
System.out.println("============================");
@@ -26,6 +36,12 @@ public static void main(String[] args) {
2636
String pathName = scan.next();
2737
String outPath = scan.next();
2838

39+
convertMethod = convertMethod.toLowerCase();
40+
if (!convertMethodSet.contains(convertMethod)){
41+
DebugUtil.logf("不存在该传换方法: ",convertMethod);
42+
continue;
43+
}
44+
2945
if (!Files.exists(Paths.get(pathName))) {
3046
DebugUtil.logf("该文件 %s 不存在", pathName);
3147
continue;
@@ -35,7 +51,8 @@ public static void main(String[] args) {
3551

3652
// 检查文件类型
3753
try {
38-
FileCheckUtil.checkFileType(convertMethod.substring(0, 3), pathName);
54+
String beforeType = convertMethod.substring(0, convertMethod.lastIndexOf("2"));
55+
FileCheckUtil.checkFileType(beforeType, pathName);
3956
} catch (Exception e) {
4057
DebugUtil.logf("请检查需要转换的文件的类型");
4158
continue;
@@ -46,8 +63,9 @@ public static void main(String[] args) {
4663
ConvertUtil.convert(convertMethod, pathName, outPath);
4764
} catch (Exception e) {
4865
DebugUtil.logf(e.getMessage());
66+
continue;
4967
}
50-
DebugUtil.logf("转换成功!文件路径: %s", outPath);
68+
DebugUtil.logf("转换成功!输出文件路径: %s", outPath);
5169
}
5270

5371
scan.close();

src/main/java/convert/FileConversion.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public interface FileConversion {
1616

1717
/**
1818
* @param pathName 源文件路径
19-
* @param outDirAndFileName 输出的文件路径和文件名
19+
* @param outPath 输出的文件路径和文件名
2020
* @return
2121
* @throws Exception
2222
*/
23-
String convert(String pathName, String outDirAndFileName) throws Exception;
23+
String convert(String pathName, String outPath) throws Exception;
2424

2525

2626
/**
@@ -30,8 +30,11 @@ public interface FileConversion {
3030
* @return
3131
* @throws Exception
3232
*/
33-
default String convert(String pathName, String outDir, String outName) throws Exception {
34-
return convert(pathName, outDir + File.separator + outName);
33+
default String convert(String pathName, String outDir, String outName,String suffix) throws Exception {
34+
if (suffix == null || "".equals(suffix) || "".equals(suffix.trim())){
35+
suffix = getSuffix();
36+
}
37+
return convert(pathName, outDir + File.separator + outName + suffix);
3538
}
3639

3740
/**
@@ -44,7 +47,7 @@ default String convert(String pathName, String outDir, String outName) throws Ex
4447
default String convert(String pathName) throws Exception {
4548
String dirName = pathName.substring(0, pathName.lastIndexOf(File.separator));
4649
String fileName = pathName.substring(pathName.lastIndexOf(File.separator) + 1, pathName.lastIndexOf("."));
47-
return convert(pathName, dirName + File.separator + fileName);
50+
return convert(pathName, dirName + File.separator + fileName + getSuffix());
4851
}
4952

5053
/**

src/main/java/convert/PDF2Image.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public boolean isSupport(String s) {
3030
}
3131

3232
@Override
33-
public String convert(String pathName, String outDirAndFileName) throws FileAlreadyExistsException {
34-
String outPath = outDirAndFileName + getSuffix();
33+
public String convert(String pathName, String outPath) throws FileAlreadyExistsException {
3534
if (Files.exists(Paths.get(outPath))) {
3635
throw new FileAlreadyExistsException(outPath + " 文件已存在");
3736
}

src/main/java/convert/PDF2Word.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import util.DebugUtil;
77

88
import java.io.*;
9+
import java.nio.charset.StandardCharsets;
910
import java.nio.file.FileAlreadyExistsException;
1011
import java.nio.file.Files;
1112
import java.nio.file.Paths;
@@ -29,8 +30,7 @@ public boolean isSupport(String s) {
2930
* @throws IOException
3031
*/
3132
@Override
32-
public String convert(String pathName, String outDirAndFileName) throws Exception {
33-
String outPath = outDirAndFileName + getSuffix();
33+
public String convert(String pathName, String outPath) throws Exception {
3434
if (Files.exists(Paths.get(outPath))) {
3535
throw new FileAlreadyExistsException(outPath + " 文件已存在");
3636
}
@@ -53,7 +53,7 @@ private void pdf2word(String pathName, String outPath) throws IOException {
5353
createFile(Paths.get(outPath));
5454

5555
FileOutputStream fos = new FileOutputStream(outPath);
56-
Writer writer = new OutputStreamWriter(fos, "UTF-8");
56+
Writer writer = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
5757
PDFTextStripper stripper = new PDFTextStripper();
5858

5959

src/main/java/convert/Word2HTML.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public boolean isSupport(String s) {
2020
}
2121

2222
@Override
23-
public String convert(String pathName, String outDirAndFileName) throws FileAlreadyExistsException {
24-
String outPath = outDirAndFileName + getSuffix();
23+
public String convert(String pathName, String outPath) throws FileAlreadyExistsException {
2524
if (Files.exists(Paths.get(outPath))) {
2625
throw new FileAlreadyExistsException(outPath + " 文件已存在");
2726
}

src/main/java/convert/Word2Image.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public boolean isSupport(String s) {
2929
}
3030

3131
@Override
32-
public String convert(String pathName, String outDirAndFileName) throws Exception {
33-
String outPath = outDirAndFileName + getSuffix();
32+
public String convert(String pathName, String outPath) throws Exception {
3433
if (Files.exists(Paths.get(outPath))) {
3534
throw new FileAlreadyExistsException(outPath + " 文件已存在");
3635
}

src/main/java/convert/Word2PDF.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public boolean isSupport(String s) {
2525
}
2626

2727
@Override
28-
public String convert(String pathName, String outDirAndFileName) throws Exception {
29-
String outPath = outDirAndFileName + getSuffix();
28+
public String convert(String pathName, String outPath) throws Exception {
3029
if (Files.exists(Paths.get(outPath))) {
3130
throw new FileAlreadyExistsException(outPath + " 文件已存在");
3231
}

src/main/java/util/ConvertUtil.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,36 @@ public class ConvertUtil {
2424

2525
public static String convert(String convertMethod, String pathName, String outPath) throws Exception {
2626

27-
String dirName;
28-
String fileName;
29-
3027
if ("null".equals(outPath)) {
3128
outPath = pathName;
29+
outPath = outPath.substring(0,outPath.lastIndexOf("."));
3230
}
3331

34-
dirName = outPath.substring(0, outPath.lastIndexOf(File.separator));
35-
36-
int last;
32+
FileConversion fileConversion = getConverter(convertMethod);
33+
if (fileConversion == null) {
34+
throw new RuntimeException("不支持该转化方法: " + convertMethod);
35+
}
3736
if (outPath.lastIndexOf(".") != -1) {
38-
last = outPath.lastIndexOf(".");
37+
// 有后缀
38+
FileCheckUtil.checkFileType(convertMethod.substring(convertMethod.lastIndexOf("2") + 1),outPath);
3939
} else {
40-
last = outPath.length();
40+
// 没有后缀加上后缀
41+
outPath = outPath + fileConversion.getSuffix();
4142
}
42-
fileName = outPath.substring(outPath.lastIndexOf(File.separator) + 1, last);
43+
// 判断输出路径是否有文件
44+
FileCheckUtil.checkFileExist(outPath);
45+
46+
outPath = fileConversion.convert(pathName, outPath);
47+
return outPath;
48+
}
4349

50+
private static FileConversion getConverter(String convertMethod) {
4451
// 调用方法
4552
for (FileConversion fileConversion : list) {
4653
if (fileConversion.isSupport(convertMethod)) {
47-
outPath = fileConversion.convert(pathName, dirName, fileName);
54+
return fileConversion;
4855
}
4956
}
50-
51-
return outPath;
57+
return null;
5258
}
5359
}

src/main/java/util/FileCheckUtil.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import execption.FileTypeError;
44

5+
import java.nio.file.FileAlreadyExistsException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
58
import java.util.HashMap;
69
import java.util.Map;
710
import java.util.regex.Matcher;
@@ -13,26 +16,37 @@
1316
public class FileCheckUtil {
1417
public static Pattern pdfPattern = Pattern.compile(".*(.pdf)$");
1518
public static Pattern wordPattern = Pattern.compile(".*(.doc|.docx)$");
19+
public static Pattern imgPattern = Pattern.compile(".*(.jpg|.jpeg|.png)$");
20+
public static Pattern htmlPattern = Pattern.compile(".*(.html)$");
1621

1722
public static Map<String, Pattern> patternMap = new HashMap<>();
1823

1924
static {
2025
patternMap.put("pdf", pdfPattern);
21-
patternMap.put("wor", wordPattern);
26+
patternMap.put("word", wordPattern);
27+
patternMap.put("image",imgPattern);
28+
patternMap.put("html",htmlPattern);
2229
}
2330

2431

2532
/**
26-
* @param pdfOrWord ‘pdf' or 'word'
33+
* @param ‘pdf' or 'word'
2734
* @param pathName 文件路径
2835
* @return
2936
*/
30-
public static void checkFileType(String pdfOrWord, String pathName) {
31-
Matcher matcher = patternMap.get(pdfOrWord).matcher(pathName);
37+
public static void checkFileType(String type, String pathName) {
38+
type = type.toLowerCase();
39+
Pattern pattern = patternMap.get(type);
40+
Matcher matcher = pattern.matcher(pathName);
3241
if (!matcher.matches()) {
33-
throw new FileTypeError("请检查文件类型");
42+
throw new FileTypeError("文件类型错误: " + pathName);
3443
}
3544
}
3645

46+
public static void checkFileExist(String path) throws FileAlreadyExistsException {
47+
if (Files.exists(Paths.get(path))) {
48+
throw new FileAlreadyExistsException(path + " 文件已存在");
49+
}
50+
}
3751

3852
}

src/test/java/convert/PDF2ImageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ public class PDF2ImageTest {
66

77
@Test
88
public void testConvert() throws Exception {
9-
new PDF2Image().convert("src/main/resources/testPdf.pdf", "src/main/resources/PDF2Image");
9+
new PDF2Image().convert("src/main/resources/testPdf.pdf", "src/main/resources/PDF2Image.jpg");
1010
}
1111
}

0 commit comments

Comments
 (0)