【问题标题】:Failed to upload WordPress media files using XML-RPC使用 XML-RPC 上传 WordPress 媒体文件失败
【发布时间】:2017-09-13 00:55:05
【问题描述】:

这是我的代码。

    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 更改代码

 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;
    }

结果

图像文件未正确显示。有什么问题?

【问题讨论】:

    标签: java wordpress spring jsp xml-rpc


    【解决方案1】:

    它需要原始图像的二进制数据。不应该是 base64 编码。 bits 参数只能是 byte[]。不要转换为字符串。

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

    希望您已经在使用org.apache.commons.io.IOUtils - 在您的代码中wpUpFile 应该只是byte[] bits

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 2019-09-25
      • 2014-12-14
      • 2014-08-11
      • 2013-07-15
      • 2017-10-03
      相关资源
      最近更新 更多