传统方式

File file = new File("/temp/1.txt");
//init array with file length
byte[] bytesArray = new byte[(int) file.length()];

FileInputStream fis = new FileInputStream(file);
fis.read(bytesArray); //read file into bytes[]
fis.close();

return bytesArray;

java.nio 方式,相对比较简洁

String filePath = "/temp/1.txt";

byte[] bFile = Files.readAllBytes(new File(filePath).toPath());
//or this
byte[] bFile = Files.readAllBytes(Paths.get(filePath));

核心代码如下:

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Function;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
public class SwaggerConfig {


    // 定义分隔符,配置Swagger多包
    private static final String splitor = ";";


    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //.apis(RequestHandlerSelectors.basePackage("com.dayu.app.controller"))
//                .apis(RequestHandlerSelectors.basePackage("com.dayu.clientweb.controller"))
                .apis(basePackage("com.dayu.app.controller"+splitor+"com.dayu.clientweb.controller"))
                .paths(PathSelectors.any())
                .build();
    }




    /**
     * 重写basePackage方法,使能够实现多包访问,复制贴上去
     * @author  teavamc
     * @date 2019/1/26
     * @return com.google.common.base.Predicate<springfox.documentation.RequestHandler>
     */
    public static Predicate<RequestHandler> basePackage(final String basePackage) {
        return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
    }


    private static Function<Class<?>, Boolean> handlerPackage(final String basePackage)     {
        return input -> {
            // 循环判断匹配
            for (String strPackage : basePackage.split(splitor)) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }


    private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }


    /**
     * 创建该API的基本信息(这些基本信息会展现在文档页面中)
     * 访问地址:http://项目实际地址/swagger-ui.html
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("客户端接口")
                .description("powered by dayu")
                //.termsOfServiceUrl("http://swagger-ui.html/")
                //.contact(contact)
                .version("1.0")
                .build();
    }


}

注意事项

  • 配置多包扫描的时候,请先确保Application 中@ComponentScan扫描到了这个包路径才行。

66742-45ew6idhdc8.png

在项目中,@Transactional(rollbackFor=Exception.class),如果类加了这个注解,那么这个类里面的方法抛出异常,就会回滚,数据库里面的数据也会回滚。

  • 用户自定义的Exception异常 属于 “ 非运行时异常”
  • 在@Transactional注解中如果不配置rollbackFor属性,那么事物只会在遇到RuntimeException的时候才会回滚。
  • 加上rollbackFor=Exception.class,可以让事物在遇到非运行时异常时也回滚

Firefox send:免费安全的文件共享

37676-nvttfss1fui.png

Mozilla 家推出的所有现代浏览器都可以使用的文件共享服务。
28433-wybetghp5h.png

主要特性
不登录最大支持 1G,登录后最大2.5GB文件
到期自焚
24 小时后自动删除,支持最大7天过期时间
加密的共享链接
支持限制下载次数
支持手动失效
分享完成后文件自动从云中删除,如此您的资料将不会永远存于云端。

官方链接直达:https://send.firefox.com/

  • 安全而私密地分享文件
  • 使用加密链接安全地发送文档和视频
  • 登录 Firefox 账户最多可达 2.5GB,并可到期自焚。
  • 分享完成后文件自动从云中删除,如此您的资料将不会永远存于云端。
免责声明
本博客部分内容来自于互联网,不代表作者的观点和立场,如若侵犯到您的权益,请联系[email protected]。我们会在24小时内进行删除。