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 0f0aeba

Browse files
file path store in DB and file is store in Directory or folder.
1 parent 4d046a8 commit 0f0aeba

File tree

10 files changed

+188
-6
lines changed

10 files changed

+188
-6
lines changed

‎springboot-file-project-upload-file-directly-in-directory-folder/pom.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@
2626
<artifactId>spring-boot-starter-web</artifactId>
2727
</dependency>
2828

29-
<dependency>
30-
<groupId>org.springframework.boot</groupId>
31-
<artifactId>spring-boot-devtools</artifactId>
32-
<scope>runtime</scope>
33-
<optional>true</optional>
34-
</dependency>
29+
3530
<dependency>
3631
<groupId>com.mysql</groupId>
3732
<artifactId>mysql-connector-j</artifactId>
@@ -47,6 +42,10 @@
4742
<artifactId>spring-boot-starter-test</artifactId>
4843
<scope>test</scope>
4944
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-devtools</artifactId>
48+
</dependency>
5049
</dependencies>
5150

5251
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.mdtalalwasim.fileproject2.controller.rest;
2+
3+
import java.io.IOException;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.MediaType;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.PostMapping;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestParam;
14+
import org.springframework.web.bind.annotation.RestController;
15+
import org.springframework.web.multipart.MultipartFile;
16+
17+
import com.mdtalalwasim.fileproject2.service.FileDataService;
18+
19+
@RestController
20+
@RequestMapping("/api")
21+
public class FileDataRestController {
22+
23+
24+
25+
@Autowired
26+
private FileDataService fileDataService;
27+
28+
@PostMapping("/file-upload-to-directory")
29+
public ResponseEntity<?> uploadImageToFileDirectory(@RequestParam("file") MultipartFile file) throws IOException{
30+
String uploadFile = fileDataService.uploadFileToFileDirectory(file);
31+
32+
return ResponseEntity.status(HttpStatus.OK).body(uploadFile);
33+
34+
35+
}
36+
37+
@GetMapping("/file-download-from-directory/{fileName}")
38+
public ResponseEntity<?> downloadImageFromFileDirectory(@PathVariable String fileName) throws IOException{
39+
byte[] downloadFile = fileDataService.downloadFileFromFileDirectory(fileName);
40+
41+
42+
43+
return ResponseEntity.status(HttpStatus.OK)
44+
.contentType(MediaType.valueOf("image/png"))
45+
.body(downloadFile);
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.mdtalalwasim.fileproject2.entity;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
import jakarta.persistence.Table;
8+
import lombok.AllArgsConstructor;
9+
import lombok.Builder;
10+
import lombok.Data;
11+
import lombok.NoArgsConstructor;
12+
13+
@Entity
14+
@Table(name = "file_data")
15+
@Data
16+
@AllArgsConstructor
17+
@NoArgsConstructor
18+
19+
@Builder
20+
public class FileData {
21+
22+
@Id
23+
@GeneratedValue(strategy = GenerationType.IDENTITY)
24+
private Long id;
25+
26+
private String name;
27+
28+
private String type;
29+
30+
private String filePath;
31+
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.mdtalalwasim.fileproject2.repository;
2+
3+
import java.util.Optional;
4+
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.stereotype.Repository;
7+
8+
import com.mdtalalwasim.fileproject2.entity.FileData;
9+
//import com.mdtalalwasim.fileproject2.service.impl.FileDataServiceImpl;
10+
11+
@Repository
12+
public interface FileDataRepository extends JpaRepository<FileData, Long>{
13+
14+
Optional<FileData> findByName(String fileName);
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.mdtalalwasim.fileproject2.service;
2+
3+
import java.io.IOException;
4+
5+
import org.springframework.web.multipart.MultipartFile;
6+
7+
public interface FileDataService {
8+
9+
String uploadFileToFileDirectory(MultipartFile file) throws IOException;
10+
11+
byte[] downloadFileFromFileDirectory(String fileName) throws IOException;
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.mdtalalwasim.fileproject2.service.impl;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.util.Optional;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Service;
9+
import org.springframework.web.multipart.MultipartFile;
10+
11+
import com.mdtalalwasim.fileproject2.entity.FileData;
12+
import com.mdtalalwasim.fileproject2.repository.FileDataRepository;
13+
import com.mdtalalwasim.fileproject2.service.FileDataService;
14+
15+
@Service
16+
public class FileDataServiceImpl implements FileDataService{
17+
18+
@Autowired
19+
private FileDataRepository fileDataRepository;
20+
21+
//private final String FILE_PATH = "/home/wasim/git/springboot-file-project-upload-file-directly-in-directory-folder/springboot-file-project-upload-file-directly-in-directory-folder/uploads/";
22+
private final String FILE_PATH = "/home/wasim/git/springboot-file-project-upload-file-directly-in-directory-folder/springboot-file-project-upload-file-directly-in-directory-folder/uploads/";
23+
24+
@Override
25+
public String uploadFileToFileDirectory(MultipartFile file) throws IOException {
26+
String filePath = FILE_PATH+file.getOriginalFilename();//absolute path
27+
// TODO Auto-generated method stub
28+
FileData fileData = fileDataRepository.save(FileData.builder()
29+
.name(file.getOriginalFilename())
30+
.type(file.getContentType())
31+
.filePath(filePath).build());
32+
33+
//copy your file into that particular path
34+
file.transferTo(new java.io.File(filePath));
35+
36+
if(fileData!= null) {
37+
return "file uploaded successfully : "+file.getOriginalFilename()+ " and Files uploaded path is :"+filePath;
38+
}
39+
return null;
40+
}
41+
42+
@Override
43+
public byte[] downloadFileFromFileDirectory(String fileName) throws IOException {
44+
// TODO Auto-generated method stub
45+
46+
47+
Optional<FileData> fileDataObj = fileDataRepository.findByName(fileName);
48+
49+
//first need to get the file path
50+
String filePath = fileDataObj.get().getFilePath();
51+
52+
//got the file, now decompress it.
53+
byte[] imageFile = Files.readAllBytes(new java.io.File(filePath).toPath());
54+
55+
return imageFile;
56+
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1+
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
2+
spring.datasource.url=jdbc:mysql://localhost:3306/file-upload-springboot-2
3+
4+
spring.datasource.username =wasim
5+
spring.datasource.password =Admin@1100
6+
spring.jpa.show-sql=true
7+
spring.jpa.hibernate.ddl-auto= update
8+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
9+
server.port=9595
10+
11+
12+
13+
14+
115

5.01 KB
Loading[フレーム]
22.9 KB
Loading[フレーム]
70.9 KB
Loading[フレーム]

0 commit comments

Comments
(0)

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