提交 0098e95e authored 作者: 朱旭欣's avatar 朱旭欣

第四次提交

上级 edf2d83c
......@@ -64,6 +64,10 @@
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
......@@ -84,6 +88,10 @@
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
</dependency>
</dependencies>
<build>
......
package com.example.tyweekly.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
......@@ -16,6 +20,8 @@ import java.io.File;
**/
@Configuration
public class WebMvcConfig {
@Value("${file.path1}")
private String save;
@Bean
public WebMvcConfigurer corsConfigurer() {
......@@ -31,11 +37,11 @@ public class WebMvcConfig {
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/file/**")
.addResourceLocations("file:" + "/root/projects/weekly/save" +File.separator);
.addResourceLocations("file:" + save );
}
......@@ -43,4 +49,19 @@ public class WebMvcConfig {
};
}
// @Bean
// public CorsWebFilter corsWebFilter(){
// CorsConfiguration configuration = new CorsConfiguration();
// configuration.addAllowedOrigin("*");
// configuration.setAllowCredentials(true);
// configuration.addAllowedMethod("*");
// configuration.addAllowedHeader("*");
//
// UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
// configurationSource.registerCorsConfiguration("/**", configuration);
//
// return new CorsWebFilter(configurationSource);
// }
}
......@@ -4,6 +4,7 @@ import com.example.tyweekly.entity.ReturnPojo;
import com.example.tyweekly.entity.WeeklyCondition;
import com.example.tyweekly.service.WeeklyFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
......@@ -11,6 +12,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
......@@ -51,11 +53,10 @@ public class WeeklyController {
}
@PostMapping("/download")
@PostMapping(value = "/download")
@ApiOperation(value = "一键下载")
public ReturnPojo downloadZip() throws FileNotFoundException {
weeklyFileService.downloadZip();
return ReturnPojo.ok("下载成功");
public HttpServletResponse downloadZip(HttpServletResponse response) throws FileNotFoundException {
return weeklyFileService.downloadZip(response);
}
......
package com.example.tyweekly.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
......@@ -7,6 +8,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
......@@ -25,12 +27,18 @@ public class WeeklyUser {
@ApiModelProperty(value = "用户主键ID",example = "1")
private Integer id;
@NotNull(message = "用户名不能为空")
@ApiModelProperty(value = "用户名称",example = "小明")
private String userName;
@ApiModelProperty(value = "文件",example = "小明")
@Transient
@JsonIgnore
private List<WeeklyFile> files;
@ApiModelProperty(value = "单个对象",example = "小明")
@Transient
private WeeklyFile fileOne;
}
......@@ -4,6 +4,7 @@ import com.example.tyweekly.entity.*;
import org.springframework.data.domain.Page;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
......@@ -38,7 +39,8 @@ public interface WeeklyFileService {
/**
* 下载
* @return
* @throws FileNotFoundException
*/
void downloadZip() throws FileNotFoundException;
HttpServletResponse downloadZip(HttpServletResponse response) throws FileNotFoundException;
}
......@@ -6,24 +6,17 @@ import com.example.tyweekly.dao.WeeklyFileDao;
import com.example.tyweekly.dao.WeeklyUserDao;
import com.example.tyweekly.entity.*;
import com.example.tyweekly.service.WeeklyFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
......@@ -53,20 +46,20 @@ public class WeeklyFileServiceImpl implements WeeklyFileService {
//MultipartFile 接收前端传过来的文件
// 注意 前端传参的name要和MultipartFile的对象名保持一直 比如此处对象名为file 则前端传参的name也要为file
//获取上传文件的文件名
String oldName=file.getOriginalFilename();
String oldName = file.getOriginalFilename();
List<WeeklyFile> all = weeklyFileDao.findAll();
//删除重名文件
for (WeeklyFile weeklyFile : all) {
if (weeklyFile.getWeeklyName().equals(oldName)){
weeklyFileDao.deleteById(weeklyFile.getId());
}
if (weeklyFile.getWeeklyName().equals(oldName)) {
weeklyFileDao.deleteById(weeklyFile.getId());
}
}
//指定上传路径
String path=save;
String path = save;
//拼接成为新文件的路径
String filePath=path+oldName;
String filePath = path + oldName;
//创建新文件对象 指定文件路径为拼接好的路径
File newFile=new File(filePath);
File newFile = new File(filePath);
//将前端传递过来的文件输送给新文件 这里需要抛出IO异常 throws IOException
file.transferTo(newFile);
//上传完成后将文件路径返回给前端用作图片回显或增加时的文件路径值等
......@@ -74,12 +67,11 @@ public class WeeklyFileServiceImpl implements WeeklyFileService {
weeklyFile1.setUploadAddress(filePath);
weeklyFile1.setWeeklyName(oldName);
weeklyFile1.setUploadTime(LocalDateTime.now());
return weeklyFileDao.save(weeklyFile1);
}
@Override
public void deleteFile(Integer id){
public void deleteFile(Integer id) {
String fileUpload = weeklyFileDao.findById(id).get().getUploadAddress();
File file = new File(fileUpload);
file.delete();
......@@ -101,7 +93,7 @@ public class WeeklyFileServiceImpl implements WeeklyFileService {
WeeklyUser weeklyUser = userMap.get(s);
List<WeeklyFile> weeklyFiles = new ArrayList<>();
weeklyFileList.forEach(weeklyFile -> {
if (weeklyFile.getWeeklyName().contains(s)){
if (weeklyFile.getWeeklyName().contains(s)) {
//如果存在
weeklyFiles.add(weeklyFile);
}
......@@ -113,38 +105,77 @@ public class WeeklyFileServiceImpl implements WeeklyFileService {
//1 上交的
Integer status = weeklyCondition.getStatus();
List<WeeklyUser> userList1 = new ArrayList<>();
switch(status){
case 0 :
switch (status) {
case 0:
//未上交
userList1 = userList.stream().filter(weeklyUser -> weeklyUser.getFiles().size() == 0).collect(Collectors.toList());
break; //可选
case 1 :
case 1:
//提交
userList1 = userList.stream().filter(weeklyUser -> weeklyUser.getFiles().size() != 0).collect(Collectors.toList());
break; //可选
//你可以有任意数量的case语句
case 2 :
case 2:
//提交
userList1 = userList;
break; //可选
default : //可选
default: //可选
userList1 = userList;
//userList
}
for (WeeklyUser weeklyUser : userList1) {
if (weeklyUser.getFiles().size() == 0) {
weeklyUser.setFiles(null);
} else {
for (WeeklyFile file : weeklyUser.getFiles()) {
weeklyUser.setFileOne(file);
}
}
}
//做物理分页
Page<WeeklyUser> perPage = PageUtil.getPerPage(of1.getPageNumber()-1, of1.getPageSize(), userList1, of1);
Page<WeeklyUser> perPage = PageUtil.getPerPage(of1.getPageNumber() - 1, of1.getPageSize(), userList1, of1);
return perPage;
}
@Override
public void downloadZip() throws FileNotFoundException {
public HttpServletResponse downloadZip(HttpServletResponse response) throws FileNotFoundException {
List<WeeklyFile> all = weeklyFileDao.findAll();
List<File> fileList = new ArrayList<>();
for (WeeklyFile weeklyFile : all) {
fileList.add(new File(weeklyFile.getUploadAddress()));
}
FileOutputStream fos2 = new FileOutputStream(new File(download));
FileOutputStream fos2 = new FileOutputStream(download);
TimedTask.toZip(fileList,fos2);
try {
File file = new File(download);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.addHeader("Access-Control-Allow-Origin", "*");
response.setContentType("application/octet-stream; charset=utf-8");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}
}
......@@ -4,10 +4,14 @@ spring.datasource.url=jdbc:mysql://192.168.100.249:3306/weekly?serverTimezone=As
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.servlet.multipart.max-file-size=1024MB
spring.servlet.multipart.max-request-size=1024MB
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
file.path=/root/projects/weekly/download/ty-weekly.zip
#file.path=/root/projects/weekly/download/ty-weekly.zip
file.path=C:/Intel/ty-weekly.zip
file.path1=/root/projects/weekly/save/
file.path1=C:/Intel/
#file.path1=/root/projects/weekly/save/
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论