๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์Šคํ”„๋ง Spring/๐Ÿงถ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ Spring

์Šคํ”„๋ง Tiles๋ฅผ ํ™œ์šฉํ•œ ๊ฒŒ์‹œํŒ ์ž๋ฃŒ์‹ค ๋งŒ๋“ค๊ธฐ (ํŒŒ์ผ ์—…๋กœ๋“œ)

by Meteora_ 2021. 6. 25.
728x90

์ถ”ํ›„ ์ฐธ๊ณ ํ•  ์‚ฌ์ดํŠธ 

์ด๋ฏธ์ง€ ์—…๋กœ๋“œ (Spring+Ajax) (aejeong.com)

 

์ด๋ฏธ์ง€ ์—…๋กœ๋“œ (Spring+Ajax)

Hyper ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋ฉด์„œ ๊ณผ์ • ์ค‘ ์–ด๋ ค์›Œํ–ˆ๋˜ ํŒŒ์ผ ์—…๋กœ๋“œ์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ ๊ณต๋ถ€ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. commons-fileupload๋ฅผ ์‚ฌ์šฉํ•˜์˜€๊ณ  pom.xml์— ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•œ๋‹ค. Ajax๋กœ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋‹ˆ Controller์— @ResponseBody๋ฅผ ๋ถ™.

aejeong.com

 

์ž๋ฃŒ์‹ค

PdsDto

 

package bit.com.a.dto;

import java.io.Serializable;

// PDS - Public Domain Software = ์ž๋ฃŒ์‹ค
public class PdsDto implements Serializable {

	private int seq;
	private String id;
	
	private String title;
	private String content;	
	private String filename;  //์‹œ์šฉ์ž๊ฐ€ ์˜ฌ๋ฆฐ ๊ธฐ์กดํŒŒ์ผ์ด๋ฆ„
	private String newFilename; //DB์ €์žฅ์‹œ ์ค‘๋ณต์„ ํ”ผํ•˜๊ธฐ์œ„ํ•ด ์—…๋กœ๋“œ๋‚ ์งœ๋กœ ๋ณ€๊ฒฝํ•  ํŒŒ์ผ์ด๋ฆ„
	
	private int readcount; //์กฐํšŒ์ˆ˜
	private int downcount;	//๋‹ค์šด๋กœ๋“œ์ˆ˜
	private String regdate; //๊ฒŒ์‹œ๋ฌผ ์ž…๋ ฅํ•œ ๋‚ ์งœ
	
	
	public PdsDto() {
	}	

	public PdsDto(int seq, String id, String title, String content, String filename, String newFilename, int readcount,
			int downcount, String regdate) {
		super();
		this.seq = seq;
		this.id = id;
		this.title = title;
		this.content = content;
		this.filename = filename;
		this.newFilename = newFilename;
		this.readcount = readcount;
		this.downcount = downcount;
		this.regdate = regdate;
	}

	public PdsDto(String id, String title, String content, String filename, String newFilename) {
		super();
		this.id = id;
		this.title = title;
		this.content = content;
		this.filename = filename;
		this.newFilename = newFilename;
	}

	public int getSeq() {
		return seq;
	}

	public void setSeq(int seq) {
		this.seq = seq;
	}

	public String getId() {
		return id;
	}

	```

Pdslist.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<table class="list_table" style="width: 85%">
<colgroup>
	<col width="50"><col width="100"><col width="300"><col width="50">
	<col width="50"><col width="50"><col width="100"><col width="50">
</colgroup>

<thead>
<tr>
	<th>๋ฒˆํ˜ธ</th><th>์ž‘์„ฑ์ž</th><th>์ œ๋ชฉ</th><th>๋‹ค์šด๋กœ๋“œ</th>
	<th>์กฐํšŒ์ˆ˜</th><th>๋‹ค์šด์ˆ˜</th><th>์ž‘์„ฑ์ผ</th><th>์‚ญ์ œ</th>
</tr>
</thead>

<tbody>

<c:forEach var="pds" items="${pdslist }" varStatus="i">

<tr>
	<th>${i.count }</th>
	<td>${pds.id }</td>
	<td style="text-align: left;">
		<a href="pdsdetail.do?seq=${pds.seq }">
			${pds.title }
		</a>
	</td>
	<td>
		<input type="button" name="btnDown" value="๋‹ค์šด๋กœ๋“œ"
			onclick="filedown('${pds.filename}','${pds.newFilename}', '${pds.seq }')">		
	</td>
	<td>${pds.readcount }</td>
	<td>${pds.downcount }</td>
	<td>
		<font size="1">${pds.regdate }</font>
	</td>
	<td>
		<img alt="" src="image/del.png" data_file_seq="${pds.seq }" class="btn_fileDelete">
	</td>
</tr>
</c:forEach>
</tbody>
</table>


<br><br>
<!--ํŽ˜์ด์ง•  -->
<div class="container">
	<nav aria-label="Page navigation">
		<ul class="pagination" id="pagination" style="justify-content:center;"></ul>
	</nav>
</div>

<br><br>
<!-- ์ž๋ฃŒ์ถ”๊ฐ€ ๋ฒ„ํŠผ -->
<div id="button.wrap">
	<span class="button blue">
		<button type="button" id="_btnAdd">์ž๋ฃŒ์ถ”๊ฐ€</button>
	</span>
</div>



<!-- ๋‹ค์šด๋กœ๋“œ ๋ฒ„ํŠผ์„ ํด๋ฆญ์‹œ -->
<form name="file_Down" action="fileDownload.do" method="post">
	<input type="hidden" name="newFilename">
	<input type="hidden" name="filename">
	<input type="hidden" name="seq">
</form>






<script>

getBbsListCount();


$("#_btnAdd").click(function () {
	location.href = "pdswrite.do";
});

function filedown(filename, newFilename, seq) {
	
	alert("๋‹ค์šด๋กœ๋“œ ํ•ฉ๋‹ˆ๋‹ค");
	let doc = document.file_Down;
	
	doc.filename.value = filename;
	doc.newFilename.value = newFilename;
	doc.seq.value = seq;
	
	
	
	doc.submit();
}





//paging ์ฒ˜๋ฆฌ
function loadPage( totalCount ) {

	let pageSize = 10;
	let nowPage = 1;
	
	let _totalPages = totalCount / pageSize;
	if(totalCount % pageSize > 0){
		_totalPages++;
	}
	
	$("#pagination").twbsPagination('destroy');	// ํŽ˜์ด์ง€ ๊ฐฑ์‹  : ํŽ˜์ด์ง•์„ ๊ฐฑ์‹ ํ•ด ์ค˜์•ผ ๋ฒˆํ˜ธ๊ฐ€ ์žฌ์„ค์ •๋œ๋‹ค.

	$("#pagination").twbsPagination({
	//	startPage: 1,
		totalPages: _totalPages,
		visiblePages: 10,
		first:'<span sria-hidden="true">«</span>',
		prev:"์ด์ „",
		next:"๋‹ค์Œ",
		last:'<span sria-hidden="true">»</span>',
		initiateStartPageClick:false,		// onPageClick ์ž๋™ ์‹คํ–‰๋˜์ง€ ์•Š๋„๋ก ํ•œ๋‹ค
		onPageClick:function(event, page){
			nowPage = page;
		//	alert('nowPage:' + page);
			getBbsListData( page - 1 );
		}
	});	
}



//๊ธ€์˜ ์ด์ˆ˜๋ฅผ ์ทจ๋“
function getBbsListCount() {
	
	$.ajax({
		url:"./pdscount.do",
		type:"get",
		data:{ page:0 },
		success:function( count ){
		//	alert('success');
		//	alert(count);
			loadPage(count);
		},
		error:function(){
			alert('error');
		}		
	});
}


</script>

PdsController

 

@Controller
public class PdsController {

	
	@Autowired
	PdsService service;
	
	
	  @RequestMapping(value="pdslist.do", method = {RequestMethod.GET,RequestMethod.POST})
	  public String pdslist(Model model) {
		  
		  model.addAttribute("doc_title", "์ž๋ฃŒ์‹ค๋ชฉ๋ก");
		  
		  List<PdsDto> list = service.getPdsList();
		  model.addAttribute("pdslist", list);
		  
		  return "pdslist.tiles";
		  
	  }

์ž๋ฃŒ ์—…๋กœ๋“œ

pdswrite.jsp

 

-์ œ๋ชฉ, ๋‚ด์šฉ ๋ฏธ์ž…๋ ฅ์‹œ ๊ธ€์ž‘์„ฑ ๋ถˆ๊ฐ€๋Šฅ(script)

-input type "file"๋กœ ํŒŒ์ผ์—…๋กœ๋“œ ๊ฐ€๋Šฅ

-enctype="multipart/form-data" ์ „์†ก

 

multipart/form-data๋Š” ํŒŒ์ผ ์—…๋กœ๋“œ๊ฐ€ ์žˆ๋Š” ์–‘์‹์š”์†Œ์— ์‚ฌ์šฉ๋˜๋Š” enctype ์†์„ฑ์˜ ๊ฐ’์ค‘ ํ•˜๋‚˜์ด๊ณ , multipart๋Š” ํผ๋ฐ์ดํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋‰˜์–ด ์„œ๋ฒ„๋กœ ์ „์†ก๋˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


<form name="frmFrom" id="_frmForm" action="pdsupload.do" method="post"
	enctype="multipart/form-data">
	
	<table class="list_table">
		<tr>
			<th>์•„์ด๋””</th>
			<td>
				<input type="text" name="id" readonly="readonly" value="${login.id} " size="50">
			</td>
		</tr>
		
		<tr>
				
				<th>์ œ๋ชฉ</th>
				<td>
					<input id="_title" type="text" name="title"  size="50">
				</td>
				
		
		</tr>
		<tr>
				
				<th>ํŒŒ์ผ ์—…๋กœ๋“œ</th>
				<td>
					<input type="file" name="fileload"  size="50">
				</td>
				
		
		</tr>
		<tr>
				
				<th>๋‚ด์šฉ</th>
				<td>
					<textarea id="_content" rows="10" cols="50" name="content"></textarea>
				</td>
				
		
		</tr>
		
		<tr>
				
				
				<td colspan="2" style="height: 50px; text-align: center">
					<a href="#none" id="_btnPds">
						<img alt="" src="image/bwrite.png">
					</a>
				</td>
				
		
		</tr>
		
		
		
	</table>
</form>

<script type="text/javascript">

$("#_btnPds").click(function () {
	
	if( $("#_title").val().trim() == "" ){
		alert("์ œ๋ชฉ์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”");
		$("#_title").focus();
	}else if($("#_content").val().trim() == ""){
		alert("๋‚ด์šฉ์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”");
		$("#_content").focus();
	}else{
		$(_frmForm).submit();
		
	}
	
});


</script>

๊ธ€์“ฐ๊ธฐ ๋ฒ„ํŠผ ๋ˆ„๋ฅผ์‹œ controller์ด๋™

 

MultipartFile์„ ์‚ฌ์šฉํ•œ File ์—…๋กœ๋“œ

(multipart/form-data)

 

 

SpringFrameworkํ™˜๊ฒฝ์˜ ์„œ๋ฒ„๋ผ๋ฉด SpringFramework์—์„œ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋Š” MultipartFile ํด๋ž˜์Šค์™€ MultipartHttpServletRequest ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์„œ File ์—…๋กœ๋“œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

์ด ๋‘๊ฐ€์ง€ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์–ด๋–ป๊ฒŒ File ์—…๋กœ๋“œ ๊ธฐ๋Šฅ์ด ๊ตฌํ˜„๋  ์ˆ˜ ์žˆ๋Š”์ง€ Controller๋กœ ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํƒ€์ž…์„ ๊ธฐ์ค€์œผ๋กœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ ๋กœ ์ œ๊ฐ€ ์ง  ์ฝ”๋“œ๋Š” FIle ์—…๋กœ๋“œ ๋ฐฉ์‹์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ HTTP ์š”์ฒญ์„ ํ•  ๋•Œ, “Content-Type” ํ•„๋“œ์˜ ๋‚ด์šฉ์„ “multipart/form-data”๋กœ ์„ธํŒ…ํ•ด์„œ ์š”์ฒญํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์คฌ์„๋•Œ

DB์— ํŒŒ์ผ์ด๋ฆ„์ด ์ค‘๋ณต๋˜์–ด ์—…๋กœ๋“œ ๋˜๋Š”๊ฒƒ์„ ๋ง‰์•„์ฃผ๊ธฐ์œ„ํ•ด ํŒŒ์ผ๋ช…์„ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ ์‚ฌ์šฉ

 

package bit.com.a.utilex;

import java.util.Date;

public class PdsUtil {
	//ํŒŒ์ผ๋ช… -> ๋ณ€๊ฒฝ(time)
	
	//myfile.txt - > f.indexOf('.') - >
	//ํŒŒ์ผ๋ช…, ํ™•์žฅ์ž๋ช…
	//f.substring(6) -> .txt
	
	
	
	public static String getNewFileName(String f) {
		String filename = "";
		String fpost = "";
		
		if(f.indexOf('.')>0) {
			//indexOf ์ง€์ •๋œ ์š”์†Œ๋ฅผ ์ฐพ๋Š”๋‹ค
			
			fpost = f.substring(f.indexOf('.')); 
			//.์˜ ์œ„์น˜๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ž๋ฅธ๋‹ค
			
			
			
			System.out.println("fpost" + fpost);
			
			filename = new Date().getTime() + fpost;
					
		}else {
			filename = new Date().getTime() + ".back";
		}
		
		return filename;
	}
	
}

PdsController @RequetParam MultipartFile ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

์š”์ฒญํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…์„ @RequestParam ์–ด๋…ธํ…Œ์ด์…˜๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ์˜ HTTP ์š”์ฒญ ๋ฉ”์‹œ์ง€์— ํฌํ•จ๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์„ ๋ชจ๋‘ ๋ถ„๋ฆฌํ•ด์„œ ์ปจํŠธ๋กคํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ปจํŠธ๋กค๋Ÿฌ์˜ ์š”์ฒญํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž… ์ค‘ MultipartFileํƒ€์ž…์— ํ•ด๋‹นํ•˜๋Š” ํ•œ๊ฐ€์ง€๋ฅผ ์ง์ ‘ ๊ณจ๋ผ์„œ, file ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์ง€๋งŒ, file ๋ฐ์ดํ„ฐ ์™ธ์— ๋‹ค๋ฅธ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค(ex, String, Integer, MultipartFile)์„ ๋ช‡๊ฐ€์ง€ ์ถ”๊ฐ€ํ•ด์„œ ํ•จ๊ป˜ ๋ฐ›๋Š”๋‹ค๋ฉด ์ฝ”๋“œ๊ฐ€ ์กฐ๊ธˆ ์ง€์ €๋ถ„ํ•ด์งˆ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

file.getOriginalFilename() : ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์—…๋กœ๋“œํ•œ ํŒŒ์ผ ์›๋ณธ ์ด๋ฆ„

 

req.getServletContext().getRealPath().getContextPath("/upload")

ํŒŒ์ผ ์—…๋กœ๋“œ ์‹œ ํ”„๋กœ์ ํŠธ ๋‚ด๋ถ€ ํด๋”๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ƒ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ๊ฐ€์ง€๊ณ  ์™”๋‹ค.

 

 @RequestMapping(value = "pdsupload.do", method = {RequestMethod.GET, RequestMethod.POST})
		public String pdsupload(PdsDto pdsdto, 
							@RequestParam(value = "fileload", required = false)
							MultipartFile fileload, 
							HttpServletRequest req) {
			
			// filename ์ทจ๋“
			String filename = fileload.getOriginalFilename();
			pdsdto.setFilename(filename);	// ์›๋ณธ ํŒŒ์ผ๋ช…์„ ์„ค์ •
			
			// upload ๊ฒฝ๋กœ ์„ค์ •
			// server(tomcat)
			String fupload = req.getServletContext().getRealPath("/upload");
			
			// ํด๋”
			// String fupload = "d:\\tmp";
			
			System.out.println("fupload:" + fupload);
			
			// ํŒŒ์ผ๋ช… ๋ณ€๊ฒฝ ์ฒ˜๋ฆฌ
			String newfilename = PdsUtil.getNewFileName(pdsdto.getFilename());		
			pdsdto.setNewFilename(newfilename);
			
			File file = new File(fupload + "/" + newfilename); 
			
			try {
				// ์‹ค์ œ๋กœ ์—…๋กœ๋“œ ๋˜๋Š” ๋ถ€๋ถ„
				FileUtils.writeByteArrayToFile(file, fileload.getBytes());
				
				// db์— ์ €์žฅ
				service.uploadPds(pdsdto);
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return "redirect:/pdslist.do";
		}

๋Œ“๊ธ€