Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- ๊น๋ฏธ๊ฒฝ์๋งํ์์
- ์นํ์ด์ง๋ง๋ค๊ธฐ
- Java
- K๋ฐฐํฐ๋ฆฌ
- ComputerScience
- ์ปดํจํฐ๊ณผํ
- css
- ์๋ฐ์คํฌ๋ฆฝํธ
- ์ฅํธ์์ค
- ๊นํ๋จ
- ๋ง์ผ๋ด๊ฐ์ธ์์๋ค์์ฐ๋ค๋ฉด
- Python
- ์ํ
- JavaScript
- ์นํผ๋ธ๋ฆฌ์ฑ
- ๊น๋ฏธ๊ฒฝ
- ์ค๋ผํด
- database
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- html
- ์ฑ
- ์ฝ๋ฉ
- ํ๋ก๊ทธ๋๋ฐ
- ๋ฐ์ํ
- ๋ ์
- ์๋ฐ
- ํ์ด์ฌ
- ๋ผํ๋ผ์ค์๋ง๋
- ๊ฐ๋ฐ
- K๋ฐฐํฐ๋ฆฌ๋ ๋ณผ๋ฃจ์
Archives
- Today
- Total
JiYoung Dev ๐ฅ
[Spring boot] ์ฒจ๋ถํ์ผ ๊ธฐ๋ฅ ์ถ๊ฐ ๋ณธ๋ฌธ
Spring boot - JPA ํ์ฉ ๊ฒ์ํ ์ฒจ๋ถํ์ผ ๊ธฐ๋ฅ ์ถ๊ฐํ๊ธฐ
FIleStore ์ฝคํฌ๋ํธ ์ถ๊ฐ
- ์ฒจ๋ถํ์ผ ์ ์ฅ ๊ฒฝ๋ก ์ง์
- ์ฒจ๋ถํ์ผ ์ด๋ฆ ๋ณํ (์์ฑ์๊ฐ ์ ๋ก๋ํ ํ์ผ๋ช → ์๋ฒ ๋ด๋ถ์์ ๊ด๋ฆฌํ๋ ํ์ผ๋ช )
- ํ์ผ ์ ์ฅ
package com.sjy.shopping.attachfile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.sjy.shopping.model.entity.UploadFile;
@Component
public class FileStore {
//๋ฃจํธ ๊ฒฝ๋ก ๋ถ๋ฌ์ค๊ธฐ
private final static String rootPath = System.getProperty("user.dir");
//ํ๋ก์ ํธ ๋ฃจํธ ๊ฒฝ๋ก์ ์๋ files ๋๋ ํ ๋ฆฌ
private final static String fileDir = rootPath + "/src/main/resources/static/files/";
public static String getPullPath(String filename) { return fileDir + filename;}
public static UploadFile storeFile(MultipartFile multipartFile) throws IOException{
if(multipartFile.isEmpty()) {
return null;
}
String originalFilename = multipartFile.getOriginalFilename();
//์์ฑ์๊ฐ ์
๋ก๋ํ ํ์ผ๋ช
-> ์๋ฒ ๋ด๋ถ์์ ๊ด๋ฆฌํ๋ ํ์ผ๋ช
์ผ๋ก ๋ณํ
//ํ์ผ๋ช
์ค๋ณต๋์ง ์๋๋ก UUID๋ก ์ ํ๊ณ ํ์ฅ์๋ ๊ทธ๋๋ก!
String storeFilename = UUID.randomUUID() + "." + extractExt(originalFilename);
//ํ์ผ์ ์ ์ฅํ๋ ๋ถ๋ถ : ํ์ผ ๊ฒฝ๋ก + storeFilename์ ์ ์ฅ
multipartFile.transferTo(new File(getPullPath(storeFilename)));
return new UploadFile(originalFilename, storeFilename);
}
// ํ์ฅ์ ์ถ์ถ
private static String extractExt(String originalFilename) {
int pos = originalFilename.lastIndexOf(".");
return originalFilename.substring(pos + 1);
}
}
์ฒจ๋ถํ์ผ Entity ์ถ๊ฐ - UploadFile
- id, ์ ๋ก๋ํ ํ์ผ๋ช , ์ ์ฅ๋ ํ์ผ๋ช ์ ์ปฌ๋ผ์ผ๋ก ๊ฐ์ง
- posts(๊ฒ์๋ฌผ) Entity์๋ id ์ปฌ๋ผ์ผ๋ก join
package com.sjy.shopping.model.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity(name="uploadfile")
public class UploadFile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "attach_id")
private Long id;
@Column(name = "upload_filename")
private String uploadFilename; //์์ฑ์๊ฐ ์
๋ก๋ํ ํ์ผ๋ช
@Column(name = "store_filename")
private String storeFilename; //์๋ฒ ๋ด๋ถ์์ ๊ด๋ฆฌํ๋ ํ์ผ๋ช
@Builder
public UploadFile(String uploadFilename, String storeFilename) {
this.uploadFilename = uploadFilename;
this.storeFilename = storeFilename;
}
}
PostController ์์ (๊ธ ๋ฑ๋ก)
- @RequestPart() MultipartFile
- ๊ธ ๋ฑ๋ก์ ์ ๋ ฅ๋ฐ์ ์ฒจ๋ถํ์ผ์ ์๋ฒ์ ์ ์ฅ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค์๋ ํ์ผ๋ช ์ ์ฅ
//๊ธ ๋ฑ๋ก
@PostMapping("/post/add")
public void post(HttpServletRequest request, HttpSession session, HttpServletResponse response, @RequestPart("attach") MultipartFile file) throws Exception {
Users user = (Users)session.getAttribute("loginUser");
//์ฒจ๋ถํ์ผ ์ฒ๋ฆฌ
UploadFile attachFile = null;
try {
attachFile = FileStore.storeFile(file);
} catch (IOException e) {
e.printStackTrace();
}
PostReqDto requestDto = new PostReqDto(request.getParameter("title"), request.getParameter("contents"), user, request.getParameter("category_name"), attachFile);
System.out.println(requestDto.getCategory_name());
log.info("view to controll with post");
postService.addPost(requestDto);
response.sendRedirect("/list");
}
PostService ์์
@Transactional
public Posts addPost(PostReqDto requestDto) {
Category category = categoryRepository.findByName(requestDto.getCategory_name());
UploadFile file = attachRepository.save(requestDto.getFile());
Posts post = new Posts(requestDto.getTitle(), requestDto.getContents(), requestDto.getUser(), category, file);
return postRepository.save(post);
}
attachRepository ์์ฑ
package com.sjy.shopping.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.sjy.shopping.model.entity.UploadFile;
public interface AttachRepository extends JpaRepository<UploadFile, Long> {
}
Front - request
form ํ๊ทธ ๋ด <input type="file"> ๋ก ํ์ผ ์ ์ก
<div class="form-group">
<label class="control-label col-sm-2" for="attach">์ฒจ๋ถํ์ผ:</label>
<div class="col-sm-10">
<input type="file" class="form-control" id="attach" name="attach">
</div>
</div>
Front - response
<tr>
<td>์ฒจ๋ถํ์ผ(ํด๋ฆญ์ ๋ค์ด)</td>
<td><a th:if="${post.file}" th:href="|/attach/${post.id}|" th:text="${post.file.uploadFilename}"/></td>
</tr>
์ฒจ๋ถํ์ผ ํด๋ฆญ์ ๋ค์ด
PostController์ ์์ฑ
//์ฒจ๋ถํ์ผ ๋ค์ด๋ก๋
@GetMapping("/attach/{id}")
public ResponseEntity<UrlResource> download(@PathVariable long id) throws MalformedURLException{
Posts post = postService.findPost(id);
String storeFilename = post.getFile().getStoreFilename();
String uploadFilename = post.getFile().getUploadFilename();
UrlResource urlResource = new UrlResource("file:" + FileStore.getPullPath(storeFilename));
//ํ๊ธ ์ธ์ฝ๋ฉ
String encodedUploadFileName = UriUtils.encode(uploadFilename, StandardCharsets.UTF_8);
String contentDisposition = "attachment; filename=\"" + encodedUploadFileName + "\"";
//header์ CONTENT_DIPOSITION ์ค์ ์ ํตํด ํด๋ฆญ์ ๋ค์ด๋ก๋ ์ค์
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition)
.body(urlResource);
}
์ฐธ๊ณ ์๋ฃ
'Question' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] List์์ ๋ฐฐ์ด(Array)๋ก ๋ณํํ๊ธฐ (0) | 2023.08.24 |
---|---|
[JAVA] List ์ ๋ ฌ - sort() (0) | 2023.08.24 |
[python] ์์์ ์๋ฆฟ์ ์ง์ (round) (0) | 2023.08.22 |
[JAVA] char ์์ ๋ฌธ์์ด๋ก ๋ณํํ๊ธฐ (0) | 2023.08.21 |
[JAVA] ๋ฐฐ์ด ํฌ๊ธฐ ๋ณ๊ฒฝํ๊ธฐ (0) | 2023.08.17 |