네트워크응용 Spring 업로드 웹사이트
2020. 4. 20. 14:08
Programing/Web Programming
build
group 'io.goorm'
version '0.0.1'
buildscript{
repositories{
jcenter();
}
dependencies{
classpath 'org.akhikhl.gretty:gretty:+'
}
}
apply plugin: 'java'
apply plugin: 'org.akhikhl.gretty'
apply plugin: 'war'
sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava.options.encoding = 'UTF-8'
repositories {
mavenCentral()
}
dependencies {
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
def webappDir = "$rootDir/src/main/webapp"
gretty{
httpPort = 8080
contextPath = "/"
servletContainer = "jetty9"
}
upload.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>FILE UPLOAD</title>
</head>
<body>
<h1>
FILE UPLOAD
</h1>
<form method="post" action="upload" enctype="multipart/form-data">
업로드 할 파일 선택 : <input type="file" name="fileupload" id="fileupload"/>
<input type="submit" value="파일전송"/>
</form>
</body>
</html>
UploadServlet.java
package iotnetwork;
import java.io.IOException;
import java.rmi.ServerException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet(name="UploadServlet",urlPatterns = {"/upload"})
@MultipartConfig(
fileSizeThreshold = 1024 * 1024 *2, //1MB
maxFileSize = 1024 * 1024 * 10, //10MB/
maxRequestSize = 1024 * 1024 * 30,
location = "/workspace/IoTNetwork/src/main/webapp/upload"
)
public class UploadServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res)throws ServerException, IOException{
res.setContentType("text/html");
req.setCharacterEncoding("UTF-8");
res.setCharacterEncoding("UTF-8");
//multipart/form-data 는 HttpServletRequest의 part형태로 전달
try{
final Part filePart = req.getPart("fileupload");
//파일 이름
final String fileName = getFileName(filePart);
System.out.println(fileName);
}catch(Exception e){
System.out.println("Error Upload");
}
}
private String getFileName(final Part part){
final String partHeader = part.getHeader("content-disposition");
System.out.println(part.getSize()+", name:"+part.getName());
System.out.println(partHeader);
for(String content: part.getHeader("content-disposition").split(";")){
if(content.trim().startsWith("filename")){
return content.substring(content.indexOf("=")*1).trim().replace("\"","");
}
}
return null;
}
}
'Programing > Web Programming' 카테고리의 다른 글
javascript function (0) | 2019.11.28 |
---|---|
javascript 배열 계산 출력 (0) | 2019.11.21 |
JavaScript prompt로 값 받아 계산하는 함수 (0) | 2019.11.21 |
PHP에서 외부 주소 JSON 언파싱 (0) | 2019.11.17 |
CSS 애니메이션 기울기, 비율, 이동 (0) | 2019.11.07 |