美文网首页
office(如:Word、Excel、PPT 等)文件轻松实现

office(如:Word、Excel、PPT 等)文件轻松实现

作者: voQuan | 来源:发表于2019-06-04 14:09 被阅读0次

    解决方案有很多,比如可以先将文件转图片或者pdf然后再网页中显示,
    我在这里说的可能并不适合大家,这里简单说下几个快捷的方式

    方案一:

    可以直接使用第三方服务,不过这个需要收费的,我在这列几个

    http://www.yozodcs.com/

    https://www.idocv.com/

    http://www.officeweb365.com/

    方案二:

    一个快速的解决方法:

    如果项目需求只要求预览内容而不要求对文档进行编辑的操作,则可以选择在线预览的方式进行处理;
    现附上两个在线预览office文档的地址:(浏览器都需要chrome的内核)

    1、 https://docs.google.com/viewer?url=(输入你的文档在服务器中的地址)

    2、 https://view.officeapps.live.com/op/view.aspx?src=(输入你的文档在服务器中的地址)

    3、 http://office.qingshanboke.com/Default.aspx?url=(输入你的文档在服务器中的地址)

    如果是本地文件可以使用下面这种方式,调用了永中的接口:

    需要用到的第三方工具包为:

    commons-logging-1.1.jar,
    httpclient-4.5.jar, 
    httpcore-4.4.1.jar,
    httpmime-4.5.jar
    

    如代码出现编译不过,请加入这四个包。

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;
    import java.nio.charset.Charset;
    import java.util.Date;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.mime.HttpMultipartMode;
    import org.apache.http.entity.mime.MultipartEntity;
    import org.apache.http.entity.mime.content.FileBody;
    import org.apache.http.entity.mime.content.StringBody;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    import com.alibaba.fastjson.JSONObject;
    
    
    public class Demo {
    
    /**
     * 向指定 URL 上传文件POST方法的请求
     *
     * @param url 发送请求的 URL
     * @param filepath 文件路径
     * @param type 转换类型
     * @return 所代表远程资源的响应结果, json数据
     */
    public static String SubmitPost(String url, String filepath, String type) {
        String requestJson = "";
        HttpClient httpclient =  HttpClients.createDefault();
        try {
            HttpPost httppost = new HttpPost(url);
            FileBody file = new FileBody(new File(filepath));
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null,
                    Charset.forName("UTF-8"));
            reqEntity.addPart("file", file); // file为请求后台的File upload;属性
            reqEntity.addPart("convertType", new StringBody(type, Charset.forName("UTF-8")));
            httppost.setEntity(reqEntity);
            HttpResponse response = httpclient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity resEntity = response.getEntity();
                requestJson = EntityUtils.toString(resEntity);
                EntityUtils.consume(resEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.getConnectionManager().shutdown();
            } catch (Exception ignore) {
    
            }
        }
        return requestJson;
    }
    
    
    public static void main(String[] args) {
        //文件上传转换,获取返回数据
        String convertByFile = SubmitPost("http://dcs.yozosoft.com:80/upload", "D://demo.ppt", "1");
        JSONObject obj = JSONObject.parseObject(convertByFile);
        if ("0".equals(obj.getString("result"))) {// 转换成功
            String urlData = obj.getString("data");
            urlData = urlData.replace("[\"", "");//去掉[
            urlData = urlData.replace("\"]", "");//去掉]
    
            //最后urlData是文件的浏览地址
            System.out.println(urlData);//打印网络文件预览地址
        } else {// 转换失败
            System.out.println("转换失败");
        }
      }
    
    }
    

    原文:https://blog.csdn.net/zhoumengshun/article/details/73382599

    相关文章

      网友评论

          本文标题:office(如:Word、Excel、PPT 等)文件轻松实现

          本文链接:https://www.haomeiwen.com/subject/poblxctx.html