Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

增加java服务器端demo #2805

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
.DS_Store
.idea
*.iml
**/target/
/pages
/node_modules
/doc
server/*
!server/php/
!server/java-spring/
!server/preview.php
!server/fileupload.php
!server/fileupload2.php
Expand Down
114 changes: 114 additions & 0 deletions server/java-spring/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?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>io.zts</groupId>
<artifactId>webuploader-server</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>webuploader-server-lib</module>
<module>webuploader-server-webdemo</module>
</modules>

<properties>
<spring.version>5.0.5.RELEASE</spring.version>
<slf4j.version>1.7.24</slf4j.version>
<log4j.version>2.8.2</log4j.version>
<thymeleaf.version>3.0.7.RELEASE</thymeleaf.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>

<dependencyManagement>
<dependencies>
<!-- spring framework start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>

</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>

</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>

</dependency>
<!-- spring framework end -->


<!-- -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.1</version>
</dependency>

<!-- test start -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- test end -->

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>


</dependencyManagement>

<dependencies>
<!--lambok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
44 changes: 44 additions & 0 deletions server/java-spring/webuploader-server-lib/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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">
<parent>
<artifactId>webuploader-server</artifactId>
<groupId>io.zts</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>

<artifactId>webuploader-server-lib</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.zts.controller;

import io.zts.controller.vo.CheckResponse;
import io.zts.controller.vo.MergeResponse;
import io.zts.controller.vo.UploadResponse;
import io.zts.controller.vo.WebuploaderResponse;
import io.zts.webuploader.service.WebuploaderService;
import io.zts.webuploader.service.enums.MergeResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/webuploader")
@Slf4j
public class WebuploaderController {

@Autowired
private WebuploaderService uploadService;


@RequestMapping(value = "/check",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public WebuploaderResponse<CheckResponse> checkChunk(
@RequestParam(name = "fileName") String filename,
@RequestParam(name="fileMd5",required = false) String fileMd5,
@RequestParam(name="chunkSize") long chunkSize,
@RequestParam(name="chunk") int chunkNum
){
boolean checkResult = this.uploadService.checkChunk(filename, chunkNum, chunkSize, fileMd5);

CheckResponse bizResponse=new CheckResponse();
bizResponse.setExists(checkResult);
return WebuploaderResponse.SUCC(bizResponse);
}

@RequestMapping(value = "/upload",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public WebuploaderResponse<UploadResponse> uploadChunk(
@RequestParam("file") CommonsMultipartFile file,
@RequestParam(value = "name") String fileName,
@RequestParam(value = "chunk", required = false, defaultValue = "0") int chunk,
@RequestParam("fileMd5") String fileMd5){

UploadResponse bizResponse=new UploadResponse();
try {
this.uploadService.saveOneChunk(fileName,file,chunk,fileMd5);
bizResponse.setUploaded(true);
return WebuploaderResponse.SUCC(bizResponse);
} catch (Exception e) {
log.error("",e);
bizResponse.setUploaded(false);
return WebuploaderResponse.FAIL(bizResponse);
}


}

@RequestMapping(value = "/merge",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public WebuploaderResponse<MergeResponse> mergeChunks(
@RequestParam(name = "fileName") String filename,
@RequestParam(name="fileMd5",required = false) String fileMd5,
@RequestParam(name="chunks") int chunk){

MergeResponse bizResponse=new MergeResponse();
bizResponse.setFileName(filename);
MergeResult result = this.uploadService.mergeChunks(filename, chunk, fileMd5, bizResponse);
if(result==MergeResult.SUCCESS){

return WebuploaderResponse.SUCC(bizResponse);
}

log.error("Merge Result={}",result);
return WebuploaderResponse.FAIL(bizResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.zts.controller.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Setter
@Getter
@JsonInclude(value=JsonInclude.Include.NON_EMPTY)

public class CheckResponse implements Serializable {

private boolean isExists;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.zts.controller.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
@JsonInclude(value=JsonInclude.Include.NON_EMPTY)
public class MergeResponse implements Serializable {

private String fileName;
private String finalName;
private String path;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.zts.controller.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Setter
@Getter
@JsonInclude(value=JsonInclude.Include.NON_EMPTY)
public class UploadResponse implements Serializable {

private boolean uploaded;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.zts.controller.vo;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Setter
@Getter
public class WebuploaderResponse<T extends Serializable> implements Serializable {

private T content;

private int code;

public static WebuploaderResponse SUCC(Serializable content){
WebuploaderResponse response=new WebuploaderResponse();
response.setContent(content);
response.setCode(0);
return response;
}

public static WebuploaderResponse FAIL(Serializable content){
WebuploaderResponse response=SUCC(content);
response.setCode(-1);
return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.zts.webuploader.config;

/**
* 配置异常
*/
public class ConfigException extends RuntimeException {

public ConfigException (String msg){
super(msg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.zts.webuploader.config;

/**
* Webuploader 配置接口
*/
public interface WebuploaderConfig {

/**
* 临时文件存储目录
* @return
*/
public String getTempDir();

/**
* 上传文件目录
* @return
*/
public String getUploaderDir();

}
Loading