| | |
| | | |
| | | import com.whyc.constant.YamlProperties; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.util.ActionUtil; |
| | | import com.whyc.util.Word2PdfJacobUtil; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import java.io.File; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.io.IOException; |
| | | import java.net.URLDecoder; |
| | | import java.util.regex.Pattern; |
| | | |
| | | @RestController |
| | | @RequestMapping("word2Pdf") |
| | |
| | | |
| | | @GetMapping("transfer") |
| | | @ApiOperation(value = "转化") |
| | | public Response transfer(@RequestParam String url,@RequestParam String fileName) throws UnsupportedEncodingException { |
| | | /*String[] urlSplit = url.split("="); |
| | | String fileUrl = urlSplit[1].replace("&fileName",""); |
| | | String fileName = urlSplit[2].split("\\.")[0];*/ |
| | | System.out.println("解码前url:"+url); |
| | | url = URLDecoder.decode(url,"utf-8"); |
| | | fileName = URLDecoder.decode(fileName,"utf-8"); |
| | | System.out.println("解码后url:"+url); |
| | | public Response transfer(@RequestParam String url,@RequestParam String fileName) throws IOException { |
| | | //过滤格式必须为 http://[ip]/zenta |
| | | //url = URLDecoder.decode(url,"utf-8"); |
| | | boolean matches = Pattern.matches("^http://(\\d{1,3}\\.){3}\\d{1,3}/zenta.*",url); |
| | | if(!matches){ |
| | | return new Response().setII(0,"接口调用异常,参数错误"); |
| | | } |
| | | //过滤特殊字符,避免路径遍历攻击 |
| | | //fileName = URLDecoder.decode(fileName,"utf-8"); |
| | | fileName = ActionUtil.filterFileName(fileName); |
| | | String fileUrl = url; |
| | | fileName = fileName.split("\\.docx")[0]; |
| | | String filePath; |
| | | //存储路径 |
| | | //项目jar同级目录下,图片能通过http方式访问到,很重要! |
| | |
| | | } |
| | | } |
| | | |
| | | Word2PdfJacobUtil.word2PDF(fileUrl,filePath+File.separator+fileName+".pdf"); |
| | | return new Response().set(1,fileName,"转化成功"); |
| | | String suffix = null; |
| | | if(fileName.contains("doc")){ |
| | | fileName = fileName.split("\\.docx")[0]; |
| | | Word2PdfJacobUtil.word2PDF(fileUrl,filePath+File.separator+fileName+".pdf"); |
| | | return new Response().set(1,fileName,"转化成功"); |
| | | } |
| | | //qi an xin 输入验证>服务器请求伪造 |
| | | /*else if(fileName.contains("xls")){ |
| | | suffix = ".xls"; |
| | | fileName = fileName.split("\\.xls")[0]; |
| | | } |
| | | else if(fileName.contains("pdf")){ |
| | | suffix = ".pdf"; |
| | | fileName = fileName.split("\\.pdf")[0]; |
| | | } |
| | | UrlDownload.downLoadFromUrl(fileUrl,fileName+suffix,filePath);*/ |
| | | return new Response().set(1,fileName+suffix,"保存成功"); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | String a = "word.docx"; |
| | | String fileName = a.split("\\.")[0]; |
| | | System.out.println(fileName); |
| | | String filePath ="F:\\zentao\\you"; |
| | | File file = new File(filePath); |
| | | boolean exists = file.exists(); |
| | | if(!exists){ |
| | | boolean mkdir = file.mkdirs(); |
| | | System.out.println(mkdir); |
| | | } |
| | | } |
| | | } |