2017-09-13 7 views
1

これは私のコードです。XML-RPCを使用してWordPressメディアファイルをアップロードできませんでした。

public void fileUpload() throws Exception { 
     byte fileByte[] =org.apache.commons.io.FileUtils.readFileToByteArray(new File(path+realName)); 
       String wpUpFile=fileToString(new File(path+realName)); 
       XmlRpcClient blog = new XmlRpcClient(); 
       XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); 
       Hashtable<String, String> post = new Hashtable<>(); 
       Vector<Serializable> params = new Vector<Serializable>(); 
       config.setServerURL(url); 
       config.setBasicUserName(user); 
       config.setBasicPassword(pw); 
       params.addElement(id); 
       params.addElement(user); 
       params.addElement(pw); 
       post.put("name", realName); 
       post.put("type", "image/jpeg"); 
       post.put("bits", wpUpFile); 
       post.put("overwrite", "false"); 
       params.addElement(post); 
       params.addElement(true); 
       Object blogPostID = blog.execute(config, "wp.uploadFile", params); 
    } 

ファイルBASE64変更コードイメージファイルが正しく表示されませんでした

enter image description here

public String fileToString(File file) throws IOException { 
       String fileString = new String(); 
       FileInputStream inputStream = null; 
       ByteArrayOutputStream byteOutStream = null; 
       try { 
        inputStream = new FileInputStream(file); 
        byteOutStream = new ByteArrayOutputStream(); 
        int len = 0; 
        byte[] buf = new byte[1024]; 
         while ((len = inputStream.read(buf)) != -1) { 
          byteOutStream.write(buf, 0, len); 
        } 
        byte[] fileArray = byteOutStream.toByteArray(); 
        fileString = new String(Base64.encodeBase64(fileArray)); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } finally { 
        inputStream.close(); 
        byteOutStream.close(); 
       } 
       return fileString; 
    } 

結果。何が問題ですか?

答えて

0

生画像のバイナリデータが必要です。 base64でエンコードするべきではありません。 bitsのパラメータはbyte[]である必要があります。 Stringに変換しないでください。

inputStream = new FileInputStream(file); 
byte[] bits = IOUtils.toByteArray(inputStream); 

は、すでにorg.apache.commons.io.IOUtilsを使用しているホープ - あなたのコードにwpUpFileはちょうどbyte[] bitsでなければなりません。

関連する問題