Spring
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>sampleBootFile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sampleBootFile</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.propertise
#port
server.port=9000
webConfig.java
package bit.com.a;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:8090");
}
}
sampleBootFileApplication.java
package bit.com.a;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SampleBootFileApplication {
public static void main(String[] args) {
SpringApplication.run(SampleBootFileApplication.class, args);
}
}
FileController.java
package bit.com.a.controller;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import bit.com.a.util.MediaTypeUtiles;
@RestController
public class FileController {
@Autowired
private ServletContext servletContext;
@RequestMapping(value="/fileUpload", method=RequestMethod.POST)
public String fileUpload(@RequestParam("uploadFile")MultipartFile uploadFile,
HttpServletRequest req){
System.out.println("FileController fileUpload");
// 경로
String upLoadPath = req.getServletContext().getRealPath("/upload");
//String upLoadPath = "d:\\tmp";
String filename = uploadFile.getOriginalFilename();
String filepath = upLoadPath + File.separator + filename; // "/"를 의미하는 것
System.out.println("path = " +filepath);
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filepath)));
bos.write(uploadFile.getBytes());
bos.close();
} catch (Exception e) {
e.printStackTrace();
return " file upload fail";
}
return "file upload success";
}
@RequestMapping(value="/download")
public ResponseEntity<InputStreamResource> download(String fileName, HttpServletRequest req)throws IOException{
System.out.println("FileController download()");
// 경로
String upLoadPath = req.getServletContext().getRealPath("/upload");
//String upLoadPath = "d:\\tmp";
MediaType mediaType = MediaTypeUtiles.getMediaTypeForFileName(this.servletContext, fileName);
System.out.println("fileName = "+ fileName);
System.out.println("mediaType = "+ mediaType);
File file = new File(upLoadPath + File.separator + fileName);
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + file.getName())
.contentType(mediaType).contentLength(file.length()).body(resource);
}
}
MediaTypeUtilse.java
package bit.com.a.util;
import javax.servlet.ServletContext;
import org.springframework.http.MediaType;
public class MediaTypeUtiles {
public static MediaType getMediaTypeForFileName(ServletContext servletContext, String filename) {
String mineType = servletContext.getMimeType(filename);
try {
MediaType mediaType = MediaType.parseMediaType(mineType);
return mediaType;
}catch(Exception e) {
return MediaType.APPLICATION_OCTET_STREAM;
}
}
}
Eclipse
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>springboot fileuplad</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>Upload file</h1>
<form id="upload_file_frm">
<input type="file" id="uploadFile" name="uploadFile">
<button type="button" id="upload_file_btn">upload</button>
</form>
<div id="here">
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#upload_file_btn").click(function(){
$.ajax({
url:"http://localhost:9000/fileUpload",
type:"POST",
data: new FormData($("#upload_file_frm")[0]),
enctype: 'multipart/form-data',
processData: false,
contentType: false,
cache: false,
success:function(){
alert("success");
},
error:function(){
alert("error");
}
});
});
});
</script>
<h1>Down load</h1>
p.png:<button type="button" id="download">Download</button>
<script type="text/javascript">
$("#download").click(function(){
/*
$.ajax({
url:"http://localhost:9000/download",
type:"GET",
data:{fileName:"Arrow.png"},
success:function(){
alert("good");
},
erorr:function(){
alert("file");
}
});
*/
location.href="http://localhost:9000/download?fileName="+"Arrow.png";
});
</script>
</body>
</html>
'Spring Boot' 카테고리의 다른 글
페이징! (0) | 2020.09.28 |
---|---|
부트 로그인 들어가기 전~ 기본셋팅 (0) | 2020.09.25 |