• 从阿里云读取文档到后台


    1. 问题 

      之前文档从阿里云读取下来,在页面展示时会出现部分中文乱码问题。乱码问题是随机出现,有时有,有时刷新一下就没有了。

    2. 问题代码

      //=========

                String content="";
                BufferedInputStream in=null;
                try {
                    URL url = new URL("https://xxxx.oss-cn-shanghai.aliyuncs.com/xxxx/news/news_body/"+id+".txt");
                   in = new BufferedInputStream(url.openStream());
                    
                    StringBuffer out = new StringBuffer(); 
                    byte[]  b = new byte[10240]; 
                    for(int n;(n=in.read(b))!=-1;){ 
                      out.append(new String(b,0,n)); 
                    } 
                    
                    content= out.toString();
                    
                    
                }catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();    
                } catch (MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
              
                
                try {
                    in.close();
                    
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //=========
           //content 为文档内容

    3. 问题解决

      文档读取用的是之前图片读取的方式。 这里需要用阿里云给的下载方法读取

      注意:

          String bucketName = "hlww"; //阿里云命名空间名
          String objectName = "hlww/news/news_body/"+id+".txt";  //文档地址

    4. 问题解决代码 

    //=========
                String content="";
                BufferedInputStream in=null;
               
                String bucketName = "hlww";
                String objectName = "hlww/news/news_body/"+id+".txt";
            //endpoint以上海为例,其它region请按实际情况填写。 String endpoint
    = "https://oss-cn-shanghai.aliyuncs.com"; // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。 String accessKeyId = "xxxxxxxxx"; String accessKeySecret ="xxxxxxxxxxxxxx"; // 创建OSSClient实例。 //OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret); OSSClient ossClient = new OSSClient(endpoint, "xxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxx"); // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。 OSSObject ossObject = ossClient.getObject(bucketName, objectName); // 读取文件内容。 System.out.println("Object content:"); BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent())); while (true) { String line=""; try { line = reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (line == null) break; content =content+line; } // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。 try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 关闭OSSClient。 ossClient.shutdown();

          //content 为文档内容
  • 相关阅读:
    冒泡法排序(整数)
    system函数的应用
    数数的位数(正整数)
    翻译Sencha Touch Architecture 第二章
    BeeFramework
    Creating a masterdetail application with tableview
    翻译Sencha Touch Mobile Javascript Framework 第八章 The Flickr Finder Application
    SDWebImage笔记
    Ubuntu aptget方式安装Subversion
    Python统计svn变更代码行数
  • 原文地址:https://www.cnblogs.com/dztHome/p/9359648.html
Copyright © 2020-2023  润新知