Java imitation Baidu Library-use swftools to Convert PDF files

Source: Internet
Author: User
Tags log log
/*****/Package com. zxjxw. framework. util; import java. io. bufferedReader; import java. io. file; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. util. arrayList; import java. util. list; import org. apache. commons. logging. log; import org. apache. commons. logging. logFactory;/*** @ author nlb * @ version 1.0 converts pdf, jpeg, font, gif, pgn, and wav Files to swf files */public class ConvertT OSwf {Log log = LogFactory. getLog (ConvertToSwf. class); private final String CONVERTFILETYPE = "pdf, jpg, jpeg, font, gif, png, wav"; private String swftoolsPath; /*** @ param swftoolsPath * is the tool address used to convert a file to swf */public ConvertToSwf (String swftoolsPath) implements this.swf toolsPath = swftoolsPath ;} /*** supports converting files into swf formats: "pdf, jpg, jpeg, font, gif, png, wav "** @ param sourceFilePath * address of the swf file to be converted * @ param swfFilePath * converted swf * @ Return */public boolean convertFileToSwf (String sourceFilePath, String swfFilePath) {log.info ("start converting files to swf format "); if (swftoolsPath = null | swftoolsPath = "") {if (log. isWarnEnabled () {log. warn ("the address of the swf conversion tool is not specified !!! ");} Return false;} String filetype = sourceFilePath. substring (sourceFilePath. lastIndexOf (". ") + 1); // determines whether the uploaded file type meets the conversion requirement to pdflog.info (" indicates that the file type passes "); if (CONVERTFILETYPE. indexOf (filetype. toLowerCase () =-1) {if (log. isWarnEnabled () {log. warn ("the current file does not meet the file type to be converted to SWF !!! ");} Return false;} File sourceFile = new File (sourceFilePath); if (! SourceFile. exists () {if (log. isWarnEnabled () {log. warn ("the file to be swf does not exist !!! ");} Return false;} log.info (" the file path to be converted exists "); if (! SwftoolsPath. endsWith (File. separator) {swftoolsPath + = File. separator;} StringBuilder commandBuidler = new StringBuilder (swftoolsPath); File swfFile = new File (swfFilePath); if (! SwfFile. getParentFile (). exists () {swfFile. getParentFile (). mkdirs ();} if (filetype. toLowerCase (). equals ("jpg") {filetype = "jpeg" ;}list <String> command = new ArrayList <String> (); command.add(this.swf toolsPath + "\" + filetype. toLowerCase () + "2swf.exe"); // read the command from the configuration file. add ("-z"); command. add ("-s"); command. add ("flashversion = 9"); command. add ("-s"); command. add ("poly2bitmap"); // add poly2bitmap to prevent large An error occurred when converting too many files or images. No exception command was generated for the swf file. add (sourceFilePath); command. add ("-o"); command. add (swfFilePath); try {ProcessBuilder processBuilder = new ProcessBuilder (); processBuilder. command (command); Process process = processBuilder. start (); log.info ("start to generate the swf file .. "); dealWith (process); try {process. waitFor (); // wait until the child process ends. The child process is the new process called by the system to convert the file} catch (InterruptedException e) {e. printStackTrace ();} File swf = New File (swfFilePath); if (! Swf. exists () {return false;} log.info ("SWF file conversion successful !!! ");} Catch (IOException e) {// TODO Auto-generated catch blocklog. error (" failed to convert to SWF file !!! "); E. printStackTrace (); return false;} return true;} public static void main (String [] args) {ConvertToSwf a = new ConvertToSwf ("D: \ Program Files \ SWFTools ");. convertFileToSwf ("D :\\ aa133", "D :\\ bb.swf");} private void dealWith (final Process pro) {// The following figure shows how to handle congestion. try {new Thread () {public void run () {BufferedReader br1 = new BufferedReader (new InputStreamReader (pro. getInputStream (); String text; try {While (text = br1.readLine ())! = Null) {System. out. println (text) ;}} catch (IOException e) {e. printStackTrace ();}}}. start ();} catch (Exception e) {e. printStackTrace ();} try {new Thread () {public void run () {BufferedReader br2 = new BufferedReader (new InputStreamReader (pro. getErrorStream (); // do not forget the information generated during processing, otherwise the String text will be blocked; try {while (text = br2.readLine ())! = Null) {System. err. println (text) ;}} catch (IOException e) {e. printStackTrace ();}}}. start ();} catch (Exception e) {e. printStackTrace ();}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.