FileUpload
package com.example.web;
import java.io.File;
import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class FileUploadController {
@Resource(name="uploadPath") //servlet-context에 정의한 uploadPath를 path에 저장하겠다
private String path;
@RequestMapping("upload")
public String upload(){
return "upload";
}
@RequestMapping( value="uploadAjax",method=RequestMethod.GET)
public String uploadAjax(){
return "uploadAjax";
}
@ResponseBody
@RequestMapping(value="uploadAjax",method=RequestMethod.POST,produces="text/plain;charset=UTF-8")
public String ajaxPost(MultipartFile file)throws Exception{
System.out.println("SIZE:" +file.getSize());
System.out.println("Type:" +file.getContentType());
String savedName=uploadFile(file.getOriginalFilename(),file.getBytes());
System.out.println("savedFile:"+savedName);
return savedName;
}
@RequestMapping(value="upload",method=RequestMethod.POST)
public void uploadPost(MultipartFile file) throws Exception{
System.out.println("upload post..............................");
System.out.println("file name:" + file.getOriginalFilename());
System.out.println("file size:" + file.getSize());
System.out.println("file type:" + file.getContentType());
System.out.println("path:"+ path);
String savedName=uploadFile(file.getOriginalFilename(),file.getBytes());
System.out.println("savedFile:"+savedName);
}
//파일이름,데이터를받아서 copy에 업로드
//실제 업로드된 파일이름을 리턴
private String uploadFile(String originalName, byte[] fileData)
throws Exception{
//이름을 유니크하게 만드는 함수
UUID uid=UUID.randomUUID();
String savedName=uid.toString()+"-"+ originalName;
File target=new File(path,savedName);
FileCopyUtils.copy(fileData, target);
return savedName;
}
@ResponseBody
@RequestMapping("deleteFile")
public void deleteFile(String fileName){
new File(path + "/"+ fileName).delete();//path 밑에 클래스를 만들어서 delete
}
}
upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>[파일업로드]</h1>
<form name="frm" action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="저장">
</form>
</body>
</html>
uploadAjax
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.js"></script>
<style>
#toupload{
width:500px;
border:1px dotted black;
padding:10px;
float:left;
margin-top:10px;
}
#uploaded{
width:500px;
border:1px dotted black;
float:right;
margin-top:10px;
}
</style>
</head>
<body>
<h2 style="float:left; margin-right:10px;">Upload AJax</h2>
<div id="toupload">
<input type="file" id="file">
</div>
<h2 style="float:right;margin-left:30px;">Uploaded</h2>
<div id="uploaded">
<ul id="uploadedFiles"></ul>
<script id="temp" type="text/x-handlebars-template">
<li>
{{fileName}}
<a href="{{fullName}}">x</a>
</li>
</script>
</div>
</body>
<script>
$("#file").on("change",function(){
var file=$("#file")[0].files[0];//파일을 여러개 선택할 수있으므로 배열에 저장한다.
var fileName=file.name;
var formData=new FormData();
formData.append("file",file);
//alert(fileName);
$.ajax({
type:"post",
url:"uploadAjax",
data:formData,
processData:false,
contentType:false,
success:function(data){
var temp=Handlebars.compile($("#temp").html());
var tempData={"fullName":data, "fileName":fileName};
$("#uploadedFiles").append(temp(tempData));
//누적:append
//new:html
}
});
});
$("#uploadedFiles").on("click","li a",function(e){
e.preventDefault();
var that=$(this);
var fileName=$(this).attr("href");
if(!confirm("삭제하시겠습니까?")) return;
$.ajax({
type:"post",
url:"deleteFile",
data:{"fileName":fileName},
success:function(){
alert("삭제되었습니다.");
that.parent().remove();
}
});
});
</script>
</html>
'spring' 카테고리의 다른 글
파일 업로드 (0) | 2019.12.05 |
---|---|
파일업로드 클래스 (0) | 2019.12.05 |
파일업로드 (0) | 2019.11.29 |
게시판에 댓글을 달면 게시글의 댓글수가 추가됩니다. (0) | 2019.11.28 |
은행계좌 프로그램 만들기 (0) | 2019.11.28 |