zoukankan      html  css  js  c++  java
  • tmp

    package hello;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.concurrent.atomic.AtomicLong;

    import org.apache.tika.Tika;
    import org.apache.tika.exception.TikaException;
    import org.apache.tika.parser.ParseContext;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.multipart.MultipartFile;
    import org.apache.tika.sax.BodyContentHandler;
    import org.apache.tika.parser.AutoDetectParser;
    import org.apache.tika.metadata.Metadata;
    import org.xml.sax.SAXException;


    @RestController
    public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
    return new Greeting(counter.incrementAndGet(),
    String.format(template, name));
    }

    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) {
    if (file.isEmpty()) {
    return "上传失败,请选择文件";
    }
    String fileName = file.getOriginalFilename();
    String filePath = "D:\";
    String fpath = filePath + "ucan" + fileName;
    File dest = new File(fpath);
    try {
    file.transferTo(dest);
    System.out.println("上传成功");
    // TD
    AutoDetectParser parser = new AutoDetectParser();
    BodyContentHandler handler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    Tika tika = new Tika();

    File f = new File("D:\1.doc"); // 声明File对象
    try (InputStream stream = new FileInputStream(f)) {
    // try (InputStream stream = input) {
    // try (InputStream stream = file.getInputStream()) {
    parser.parse(stream, handler, metadata);
    String s = handler.toString();
    System.out.println(" handler.toString()" + s);
    System.out.println(" tika.parseToString(stream)" + tika.parseToString(stream));
    } catch (IOException | SAXException | TikaException e) {
    System.out.println(e);
    }

    return "上传成功";
    } catch (IOException e) {
    System.out.println(e);
    }
    return "上传失败!";
    }
    }


    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Tg</groupId>
    <artifactId>Ta</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    </parent>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-core</artifactId>
    <version>1.22</version>
    </dependency>
    </dependencies>

    <properties>
    <java.version>1.8</java.version>
    </properties>
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>
    </project>


  • 相关阅读:
    转 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
    call_grant_sel.sql
    sql server 2008 评估期已过期解决办法
    (2.3)学习笔记之mysql基础操作(表/库操作)
    (2.2)学习笔记之mysql基础操作(登录及账户权限设置)
    Red Gate系列
    教你使用SQL数据库索引(1-15)
    cache与buffer的区别
    (4.17)什么情况下应该使用索引
    windows server2003/2008中权限账户
  • 原文地址:https://www.cnblogs.com/rsapaper/p/11504216.html
Copyright © 2011-2022 走看看