java - 上传图片到又拍云上
ringa_lee
ringa_lee 2017-04-17 11:19:53

项目部署到百度bae上,上传图片到又拍云上。

我采用了两种方式。

  1. 上传图片到又拍云上的图片空间。

这个时候,又拍云上的示例代码是直接把本地的图片上传到又拍云的。
但是我试着这样做的时候。由于bae的限制,好像用户上传的图片不能上传到bae上,形成图片。
这样的话,那我无法用又拍云上的代码,结果我尝试着用图片二进制来传。结果出现了not a picture file的错误。这种方法不行。。。。

    ServletFileUpload upload = new ServletFileUpload();
            FileItemIterator iterator = upload.getItemIterator(request);
            String path = request.getContextPath();
            String basePath = request.getScheme() + "://"
                        + request.getServerName() 
                        + path + "/";
            int size = 0;
            if (iterator.hasNext())
            {
                FileItemStream item = iterator.next();
                System.out.println("fieldName:" + item.getFieldName());
                if (item.getName() != null)
                {
                    pic_name = item.getName();
                    valuess = read(item.openStream());
                    File apple=new File(basePath+"images/"+pic_name);
                    FileOutputStream fos=new FileOutputStream(apple);
                    fos.write(valuess.getBytes("utf-8"));
                    fos.flush();
                    fos.close();
                }
            }
            sample_pic_file =basePath +"images/"+pic_name;
            upyun = new UpYun(BUCKET_NAME, USER_NAME, USER_PWD);
            upyun.setDebug(true);
            boolean test=testGmkerl();

        public static boolean testGmkerl() throws IOException
    {

        // 要传到upyun后的文件路径
        String filePath = DIR_ROOT + pic_name;

        // 本地待上传的图片文件
        File file = new File(sample_pic_file);

        // 设置缩略图的参数
        Map<String, String> params = new HashMap<String, String>();

        // 设置缩略图类型,必须搭配缩略图参数值(KEY_VALUE)使用,否则无效
        params.put(PARAMS.KEY_X_GMKERL_TYPE.getValue(),
                PARAMS.VALUE_FIX_BOTH.getValue());

        // 设置缩略图参数值,必须搭配缩略图类型(KEY_TYPE)使用,否则无效
        params.put(PARAMS.KEY_X_GMKERL_VALUE.getValue(), "128x128");

        // 设置缩略图的质量,默认 95
        params.put(PARAMS.KEY_X_GMKERL_QUALITY.getValue(), "95");

        // 设置缩略图的锐化,默认锐化(true)
        params.put(PARAMS.KEY_X_GMKERL_UNSHARP.getValue(), "true");

        // 若在 upyun 后台配置过缩略图版本号,则可以设置缩略图的版本名称
        // 注意:只有存在缩略图版本名称,才会按照配置参数制作缩略图,否则无效
        params.put(PARAMS.KEY_X_GMKERL_THUMBNAIL.getValue(), "small");


        // 上传文件,并自动创建父级目录(最多10级)
        boolean result = upyun.writeFile(filePath, file, true, params);

        System.out.println();
        return result;
    }

2.上传图片到又拍云上的文件空间。

这样我做的话,也是上传图片二进制,上传成功了,但是图片显示不出来。
这个文件空间,难道只能存文本?

示列代码:

public static void testWriteFile() throws IOException {

    // 要上传的纯文字内容
    String content = "test content";

    // 要传到upyun后的文件路径
    String filePath = DIR_ROOT + FILE_NAME;
    // 要传到upyun后的文件路径:多级目录
    String filePath2 = DIR_MORE + FILE_NAME;

    /*
     * 上传方法1:文本内容直接上传
     */
    boolean result1 = upyun.writeFile(filePath, content);
    System.out.println("1.上传 " + filePath + isSuccess(result1));

}

这个问题不好解决,望有能力人解决下。

ringa_lee
ringa_lee

ringa_lee

Popular topics
More>
Latest downloads
More>
web effects
Website source code
Website materials
Front end template
About us Disclaimer Sitemap
PHP Chinese website:Public welfare online PHP training,Help PHP learners grow quickly!