前端上传pdf文件
html
html
<form class="layui-form">
<div style="background-color: #ffffff" >
<div style="padding: 30px">
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label"><label style="color: red">*</label>编号:</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="orderNo" id="orderNo" >
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label "><label style="color: red">*</label>上传文件:</label>
<div class="layui-inline layui-input-inline">
<input type="file" name="printFile" id="printFile">
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button type="button" class="layui-btn layui-btn-sm" id="save-button" onclick="save()">提交</button>
</div>
</div>
</div>
</div>
</form>
js
js
function save(){
var formData = new FormData();
formData.appen("orderNo",$("#orderNo").val())
var files = document.getElementById('printFile');
if(files.files && files.files.length > 1){
formData.append('file',files.files[0])
}else{
layer.msg("文件不能为空");
return false;
}
formData.append('file',files.files[0])
console.log("formData",formData)
var url = ""
HSoft.postMultipartForm(url), formData,function(res){
console.log(res)
if ("200" == res.code) {
layer.msg(res.data);
setTimeout(function(){
layer.closeAll('loading');
},1000)
} else {
layer.msg("提交失败",res.msg);
}
});
}
this.postMultipartForm = function(url,formData,fn,errorCallback){
$.ajax({
type: "POST",
url: url,
data: formData,
/**
*必须false才会自动加上正确的Content-Type
*/
contentType: false,
/**
* 必须false才会避开jQuery对 formdata 的默认处理
* XMLHttpRequest会对 formdata 进行正确的处理
*/
processData: false,
error: function (XHR, textStatus, errorThrown) {
console.log("XHR=" + XHR + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);
if (errorCallback && typeof errorCallback == "function") {
errorCallback({});
}
},
success: function (data, textStatus) {
if (fn && typeof fn == "function") {
fn(data);
}
}
});
};
后台接收文件
安装pdfbox依赖
xml
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.27</version>
</dependency>
controller
java
@PostMapping("/uploadFile")
@ApiOperation("上传文件")
@ResponseBody
public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request){
DataRespVO respVO = orderService.uploadFile(file,request);
if(Objects.equals(respVO.getCode(), 200)){
return CommonResult.success(respVO.getMessage());
}
return CommonResult.error(respVO.getCode(), respVO.getMessage());
}
service
java
@Override
public DataRespVO uploadFile(MultipartFile file, HttpServletRequest request) {
MultipartHttpServletRequest params=((MultipartHttpServletRequest) request);
String orderNo = params.getParameter("orderNo ");
if(HSoft.isEmpty(orderNo )){
return new DataRespVO(ErrorCodeConstants.ORDER_NO_IS_EMPTY);
}
PDDocument document = null;
try{
// 读取标签文件内容
String fileName = file.getOriginalFilename();
document = PDDocument.load(file.getInputStream());
int pageSize = document.getNumberOfPages();
String text = "";
// 一页一页读取
for (int i = 0; i < pageSize; i++) {
// 文本内容
PDFTextStripper stripper = new PDFTextStripper();
// 设置按顺序输出
stripper.setSortByPosition(true);
stripper.setStartPage(i + 1);
stripper.setEndPage(i + 1);
text = stripper.getText(document);
System.out.println(text.trim());
}
}catch (Exception e){
log.error(e.getMessage(),e);
return new DataRespVO((new ErrorCode(10000001,"数据更新失败,"+e.getMessage())));
} finally {
try {
if (document != null) {
document.close();
}
} catch (IOException e) {
log.error(e.getMessage(),e);
return new DataRespVO((new ErrorCode(10000001,"数据更新失败,"+e.getMessage())));
}
}
return new DataRespVO(ErrorCodeConstants.UPDATE_OK);
}