Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 265f3f0

Browse files
committed
20221116 file down기능 수정, batch 추가
1 parent 756180a commit 265f3f0

File tree

8 files changed

+114
-140
lines changed

8 files changed

+114
-140
lines changed

‎build.gradle‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
1-
/*
2-
* This file was generated by the Gradle 'init' task.
3-
*
4-
* This project uses @Incubating APIs which are subject to change.
5-
*/
6-
71
plugins {
8-
id 'java'
9-
id 'org.springframework.boot' version '2.7.5'
2+
id 'java'
3+
id 'org.springframework.boot' version '2.7.5'
104
}
115

126
apply plugin: 'java'
137
apply plugin: 'io.spring.dependency-management'
148

9+
1510
repositories {
1611
mavenCentral()
1712
maven {
1813
url = uri('https://repo.maven.apache.org/maven2/')
1914
}
2015
}
21-
2216
dependencies {
23-
implementation 'org.springframework.boot:spring-boot-starter-web'
17+
implementation 'org.springframework.boot:spring-boot-starter-web'
2418
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
2519
implementation 'org.springframework.boot:spring-boot-starter-security'
2620
implementation 'org.springframework.boot:spring-boot-starter-aop'
2721
implementation 'org.springframework.boot:spring-boot-starter-actuator'
2822
implementation 'org.springframework.boot:spring-boot-starter-validation'
2923
implementation 'org.springframework.boot:spring-boot-starter-websocket'
24+
implementation 'org.springframework.boot:spring-boot-starter-quartz'
3025
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0'
3126
implementation 'com.oracle.database.jdbc:ojdbc8:21.3.0.0'
3227
implementation 'org.apache.commons:commons-lang3:3.12.0'
@@ -39,6 +34,7 @@ dependencies {
3934
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper:9.0.68'
4035
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.261'
4136
implementation 'javax.servlet:jstl:1.2'
37+
implementation 'org.quartz-scheduler:quartz:2.3.0'
4238
testImplementation 'org.springframework.boot:spring-boot-starter-test'
4339
}
4440

‎src/main/java/com/singer/application/controller/sf/SF01Controller.java‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@
88

99
import lombok.extern.slf4j.Slf4j;
1010

11-
import java.util.HashMap;
11+
import java.io.File;
12+
import java.nio.file.Files;
13+
import java.nio.file.Paths;
1214

1315
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.core.io.InputStreamResource;
17+
import org.springframework.core.io.Resource;
18+
1419
import javax.servlet.http.HttpServletRequest;
1520
import javax.validation.Valid;
1621

22+
import org.springframework.http.ContentDisposition;
23+
import org.springframework.http.HttpHeaders;
1724
import org.springframework.http.HttpStatus;
1825
import org.springframework.http.ResponseEntity;
1926
import org.springframework.stereotype.Controller;
@@ -99,14 +106,18 @@ public ResponseEntity<SF01Response> selectOneSF01Vo(@PathVariable int seq, HttpS
99106

100107
@ResponseBody
101108
@RequestMapping(value = "/sf01/file/{seq}", method = RequestMethod.GET)
102-
public ModelAndView selectFileSF01Vo(@PathVariable int seq, HttpServletRequest request) throws Exception {
109+
public ResponseEntity<Object> selectFileSF01Vo(@PathVariable int seq, HttpServletRequest request) throws Exception {
103110
log.debug("enter sf01File get");
104111

105112
String userid = getSessionId(request);
106-
HashMap<String, Object> downloadFile = sf01Service.selectFile(seq, userid);
113+
File file = sf01Service.selectFile(seq, userid);
114+
Resource resource = new InputStreamResource(Files.newInputStream(Paths.get(file.getPath())));
115+
116+
HttpHeaders headers = new HttpHeaders();
117+
headers.setContentDisposition(ContentDisposition.builder("attachment").filename(file.getName()).build());
107118

108119
log.debug("exit sf01File get");
109-
return new ModelAndView("filedownloadView", "downloadFile", downloadFile);
120+
return new ResponseEntity<Object>(resource, headers, HttpStatus.OK);
110121
}
111122

112123
@ResponseBody

‎src/main/java/com/singer/application/service/sf/SF01Service.java‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.singer.application.dto.sf.SF01Response;
77
import java.io.File;
88
import java.io.InputStream;
9-
import java.util.HashMap;
109
import java.util.Iterator;
1110
import java.util.List;
1211
import org.springframework.beans.factory.annotation.Autowired;
@@ -170,7 +169,7 @@ public int deleteSF01(int seq, String sessionid) throws Exception {
170169
return sf01Dao.deleteSF01Vo(sf01Entity);
171170
}
172171

173-
public HashMap<String, Object> selectFile(int seq, String userid) throws Exception {
172+
public File selectFile(int seq, String userid) throws Exception {
174173

175174
SF01Entity sf01Entity = new SF01Entity();
176175
sf01Entity.setSeq(seq);
@@ -183,16 +182,11 @@ public HashMap<String, Object> selectFile(int seq, String userid) throws Excepti
183182
File file = new File(s3Properties.getTempPath() + "/" + filename);
184183
FileUtils.copyInputStreamToFile(inputStream, file);
185184

186-
HashMap<String, Object> hashMap = new HashMap<String, Object>();
187-
188-
hashMap.put("downfile", file);
189-
hashMap.put("filename", sf01Entity.getFilename());
190-
191185
sf01Entity.setDownuserid(userid);
192186
sf01Entity.setRegdate(DateUtil.getTodayTime());
193187
sf01Dao.mergeSFD1Vo(sf01Entity);
194188

195-
return hashMap;
189+
return file;
196190
}
197191

198192
}

‎src/main/java/com/singer/common/bean/ViewConfigs.java‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.singer.common.bean;
22

33
import com.fasterxml.jackson.core.JsonEncoding;
4-
import com.singer.common.view.BlobDownloadView;
5-
import com.singer.common.view.FileDownloadView;
64
import org.springframework.context.annotation.Bean;
75
import org.springframework.context.annotation.Configuration;
86
import org.springframework.http.MediaType;
@@ -27,13 +25,4 @@ public MappingJackson2JsonView jsonView() {
2725
return jsonView;
2826
}
2927

30-
@Bean(name = "filedownloadView")
31-
public FileDownloadView fileDownloadView() {
32-
return new FileDownloadView();
33-
}
34-
35-
@Bean(name = "blobdownloadView")
36-
public BlobDownloadView blobDownloadView() {
37-
return new BlobDownloadView();
38-
}
3928
}

‎src/main/java/com/singer/common/view/BlobDownloadView.java‎

Lines changed: 0 additions & 52 deletions
This file was deleted.

‎src/main/java/com/singer/common/view/FileDownloadView.java‎

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.singer.infrastructure.batch;
2+
3+
import org.quartz.Trigger;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
7+
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
8+
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
9+
10+
@Configuration
11+
public class BatchConfig {
12+
@Bean
13+
public JobDetailFactoryBean jobDetailFactoryBean() {
14+
JobDetailFactoryBean jobBean = new JobDetailFactoryBean();
15+
jobBean.setJobClass(DeleteFileBatch.class);
16+
jobBean.setDurability(true);
17+
return jobBean;
18+
}
19+
20+
@Bean
21+
public CronTriggerFactoryBean cronTriggerFactoryBean() {
22+
CronTriggerFactoryBean cronBean = new CronTriggerFactoryBean();
23+
cronBean.setJobDetail(jobDetailFactoryBean().getObject());
24+
cronBean.setCronExpression("0 0/30 * * * ?");
25+
return cronBean;
26+
}
27+
28+
@Bean
29+
public SchedulerFactoryBean schedulerFactoryBean() {
30+
SchedulerFactoryBean scheduleBean = new SchedulerFactoryBean();
31+
Trigger[] triggers = new Trigger[1];
32+
triggers[0] = cronTriggerFactoryBean().getObject();
33+
scheduleBean.setTriggers(triggers);
34+
return scheduleBean;
35+
}
36+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.singer.infrastructure.batch;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
import java.util.Iterator;
8+
import java.util.function.Consumer;
9+
import java.util.stream.Stream;
10+
11+
import org.quartz.DisallowConcurrentExecution;
12+
import org.quartz.JobExecutionContext;
13+
import org.quartz.JobExecutionException;
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.scheduling.quartz.QuartzJobBean;
16+
import org.springframework.stereotype.Component;
17+
18+
import com.singer.infrastructure.config.S3Properties;
19+
20+
import lombok.Setter;
21+
import lombok.extern.slf4j.Slf4j;
22+
23+
@Setter
24+
@Component
25+
@DisallowConcurrentExecution
26+
@Slf4j
27+
public class DeleteFileBatch extends QuartzJobBean {
28+
29+
@Autowired
30+
private S3Properties s3Properties;
31+
32+
@Override
33+
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
34+
String path = s3Properties.getTempPath();
35+
Path deletePath = Paths.get(path);
36+
Consumer<Path> consumer = filePath -> {
37+
Stream<Path> stream;
38+
try {
39+
stream = Files.list(deletePath);
40+
Iterator<Path> files = stream.iterator();
41+
while (files.hasNext()) {
42+
Path file = files.next();
43+
log.debug("deleted file name " + file.getFileName());
44+
if (!Files.isDirectory(file)) {
45+
Files.delete(file);
46+
}
47+
}
48+
} catch (IOException e) {
49+
log.error(e.getMessage());
50+
}
51+
};
52+
consumer.accept(deletePath);
53+
54+
}
55+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /