我想在服务器上使用PDFBox将我的pdf解析为txt文件。我在我的控制器上调用解析服务。我有一个上传文件的动作,一旦上传,我将解析服务添加到txt中,以便在我的saveDirectoryFolder上处理我的文本文件。
@RequestMapping(value = "/savefiles", method = RequestMethod.POST)
public String crunchifySave(
@ModelAttribute("uploadForm") irFileUpload uploadForm,
Model map) throws IllegalStateException, IOException {
String saveDirectory = "C://raexFiles//";
List<MultipartFile> crunchifyFiles = uploadForm.getFiles();
List<String> fileNames = new ArrayList<String>();
if (null != crunchifyFiles && crunchifyFiles.size() > 0) {
for (MultipartFile multipartFile : crunchifyFiles) {
String fileName = multipartFile.getOriginalFilename();
if (!"".equalsIgnoreCase(fileName)) {
// Handle file content - multipartFile.getInputStream()
File myfile = new File(saveDirectory + fileName);
multipartFile.transferTo(myfile);
// Here I call the service of the parse
parse.toTxt(myfile , saveDirectory + fileName);
fileNames.add(fileName);
}
}
}
map.addAttribute("files", fileNames);
return "uploadfile";
}
}
我的解析服务有一个带有两个属性的方法(文件和txt文件的目标):
@Service("PaseService")
@Transactional
public class ParseServiceImpl implements ParseService{
String content;
@Override
public void toTxt(File file, String pathFile) throws IOException {
PDDocument document = PDDocument.load(file);
PDFTextStripper s = new PDFTextStripper();
content = s.getText(document);
// Parser La Sortie Dans Un Fichier txt
try( PrintWriter out = new PrintWriter(new File(pathFile + ".txt"))){
out.println(content);
}
}
}
任何人都可以看到我的代码存在问题