app-文档预览

文档预览

需求及现状

需求:

office文档、PDF文件、图片要求实现预览,不能下载下来通过手机本地wps或者pdf阅读器再打开,用户体验不好。

现状:

文件存储在另外的服务器。
无现成的文档预览组件,需要从头开发。

思路

Created with Raphaël 2.1.0 文档下载 文档转换(office转换为pdf) 文档展示(pdf展示)

关键技术

1.下载文件

代码
/***
     * 
     * @throws MalformedURLException
     */
    public String downloadNet(String address, String filename) throws MalformedURLException {
        // 下载网络文件
        String result = "";
        int byteread = 0;
        /*
         * String urlStr = ""; try { urlStr = URLEncoder.encode(URL_75 +
         * address, "utf-8"); } catch (UnsupportedEncodingException e1) { //
         * TODO 自动生成的 catch 块 e1.printStackTrace(); }
         */
        URL url = new URL(address);
        InputStream inStream = null;
        FileOutputStream fs = null;
        try {
            URLConnection conn = url.openConnection();

            HttpURLConnection httpURLConnection = (HttpURLConnection) conn;

            httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
            httpURLConnection.setRequestProperty("Content-Type", "text/html,application/xhtml+xml,application/xml;");
            httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
            inStream = httpURLConnection.getInputStream();
            fs = new FileOutputStream(filename);
            byte[] buffer = new byte[1204];
            while ((byteread = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteread);
            }
            result = filename;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fs != null)
                    fs.close();
                if (inStream != null)
                    inStream.close();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块

            }

        }
        return result;
    }

inStream = httpURLConnection.getInputStream();

报错:

Server returned HTTP response code: 400

错误分析:

肯定是传递的url不被另一边的服务器识别,造成无法访问。

解决思路:

将url进行编码,通过对比在浏览器中的url路径,发现只要把汉语部分编码为utf-8即可。
在网上搜索了半天,找到了实现代码:

public class Encoder {
    public static String Encode(String text, String Endocing) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c >= 0 && c <= 255) {
                result.append(c);
            } else {
                byte[] b = new byte[0];
                try {
                    b = Character.toString(c).getBytes(Endocing);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                for (int j = 0; j < b.length; j++) {
                    int k = b[j];
                    if (k < 0)
                        k += 256;
                    result.append("%" + Integer.toHexString(k).toUpperCase());
                }
            }
        }
        return result.toString();
    }

    public static String EncodeKeyWords(String Words, String Encoding) {
        String Codes = "";
        String Word = "";
        int i = 0;
        Words += ' ';
        while (i < Words.length()) {
            if (Words.charAt(i) != ' ') {
                Word += Words.charAt(i);
            } else {
                if (i != Words.length() - 1) {
                    Codes += Encode(Word, Encoding) + "%20";
                }
                else
                {
                    Codes += Encode(Word, Encoding) + "+";
                }
                Word = "";
            }
            i++;
        }
        Codes = Codes.substring(0, Codes.length() - 1);
        return Codes;
    }
}

调用方法

String encodeUrl = Encoder.EncodeKeyWords("http://xxxxxxx:xxxx/UploadFile/yzsq/" + "汉字 (1)", "utf-8");

2.转换文档

没有现成的实现代码,所以从网上各种搜索。
搜的一个office转pdf,先临时用着。
下载地址:http://download.csdn.net/detail/u010120191/6994031
存在不足:office2007及以上就不能转换了,需要再想解决方法。

3.pdf展示

利用开源组件pdf.js来实现。
看起来还是很强大的。目前只是能用起来了,还没研究代码。

*最终代码见《用印管理》

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值