分类 spring articles

添加第三方jar到Spring Boot application

背景

有时候需要添加第三方的jar包到spring boot工程中,因为不能从maven repository直接下载,所以需要包含在代码库中。 一般需要在

src/main

目录下,可以建个新目录 lib, 然后把jar文件放到这个目录下,也就是 src/main/lib下.

然后在pom.xml中需要添加

  • groupId/artifactId/version/scope都需要填上
        <dependency>
            <groupId>com.ibm.bluepages</groupId>
            <artifactId>bpjtk</artifactId>
            <version>3.0.6.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/lib/bpjtk-v3.0.6.0.jar</systemPath>
        </dependency>
  • 在 spring-boot-maven-plugin中加上 includeSystemScope true
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                  <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
……

Continue reading

Vue File Download From Spring Boot

File download

如果spring boot的写法是我在另外一个文章, 返回的response其实是一个Blob, 这一点要记住

          this.axios.get(url, { responseType: 'blob', timeout: 0 }).then(response => {
              // response is Blob <-------
                const url = window.URL.createObjectURL(response)
                const a = document.createElement('a')
                a.style.display = 'none'
                a.href = url
                a.setAttribute('download','file.xlsx')
                document.body.appendChild(a)
                a.click()
                document.body.removeChild(a);
                window.URL.revokeObjectURL(url);
          });
……

Continue reading

Spring Boot File Download

spring boot application

提供下载的代码,可以返回一个Resource

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class ReportController {



    @GetMapping(value = "/filedownload")
    public ResponseEntity<InputStreamResource> downloadFile(HttpServletRequest request) {

        // .......................
        // .......................
        // .......................

        ByteArrayInputStream stream = xxxxxx;// get the stream

        return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=file.xlsx")
                .body(new InputStreamResource(stream));

    }

}
……

Continue reading