<BaseController>

package com.myshop.common.base;

import com.myshop.goods.vo.ImageFileVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

public class BaseController {
	private static final String CURR_IMAGE_REPO_PATH = "C:\\shopping\\file_repo";

	/* 여러개의 파일을 사용하기 위해 MultipartHttpServletRequest을 사용한다. */
	protected List<ImageFileVO> upload(MultipartHttpServletRequest multipartRequest) throws Exception{
		/* fileList에 ImageFileVO 리스트를 저장한다. */
		List<ImageFileVO> fileList= new ArrayList<ImageFileVO>();

		/* Iterator는 인터페이스로 fileName에 파일 이름들을 저장한다. */
		Iterator<String> fileNames = multipartRequest.getFileNames();

		/* Iterator의 hashNext() 메소드를 사용하여 하나씩 비교한다. */
		while(fileNames.hasNext()){
			ImageFileVO imageFileVO =new ImageFileVO();

			/* Iterator에 next() 메소드를 사용하여  fileName 변수에 저장한다. */
			String fileName = fileNames.next();

			/* VO에 파일 타입을 저장한다. */
			imageFileVO.setFileType(fileName);
			MultipartFile mFile = multipartRequest.getFile(fileName);
			String originalFileName=mFile.getOriginalFilename();
			imageFileVO.setFileName(originalFileName);
			fileList.add(imageFileVO);

			File file = new File(CURR_IMAGE_REPO_PATH +"\\"+ fileName);
			if(mFile.getSize()!=0){ //File Null Check
				if(! file.exists()){ //��λ� ������ �������� ���� ���
					if(file.getParentFile().mkdirs()){ //��ο� �ش��ϴ� ���丮���� ����
						file.createNewFile(); //���� ���� ����
					}
				}
				mFile.transferTo(new File(CURR_IMAGE_REPO_PATH +"\\"+"temp"+ "\\"+originalFileName)); //�ӽ÷� ����� multipartFile�� ���� ���Ϸ� ����
			}
		}
		return fileList;
	}

	private void deleteFile(String fileName) {
		File file =new File(CURR_IMAGE_REPO_PATH+"\\"+fileName);
		try{
			file.delete();
		}catch(Exception e){
			e.printStackTrace();
		}
	}


	@RequestMapping(value="/*.do" ,method={RequestMethod.POST,RequestMethod.GET})
	protected ModelAndView viewForm(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String viewName=(String)request.getAttribute("viewName");
		ModelAndView mav = new ModelAndView(viewName);
		return mav;
	}


	protected String calcSearchPeriod(String fixedSearchPeriod){
		String beginDate=null;
		String endDate=null;
		String endYear=null;
		String endMonth=null;
		String endDay=null;
		String beginYear=null;
		String beginMonth=null;
		String beginDay=null;
		DecimalFormat df = new DecimalFormat("00");
		Calendar cal=Calendar.getInstance();

		endYear   = Integer.toString(cal.get(Calendar.YEAR));
		endMonth  = df.format(cal.get(Calendar.MONTH) + 1);
		endDay   = df.format(cal.get(Calendar.DATE));
		endDate = endYear +"-"+ endMonth +"-"+endDay;

		if(fixedSearchPeriod == null) {
			cal.add(cal.MONTH,-4);
		}else if(fixedSearchPeriod.equals("one_week")) {
			cal.add(Calendar.DAY_OF_YEAR, -7);
		}else if(fixedSearchPeriod.equals("two_week")) {
			cal.add(Calendar.DAY_OF_YEAR, -14);
		}else if(fixedSearchPeriod.equals("one_month")) {
			cal.add(cal.MONTH,-1);
		}else if(fixedSearchPeriod.equals("two_month")) {
			cal.add(cal.MONTH,-2);
		}else if(fixedSearchPeriod.equals("three_month")) {
			cal.add(cal.MONTH,-3);
		}else if(fixedSearchPeriod.equals("four_month")) {
			cal.add(cal.MONTH,-4);
		}

		beginYear   = Integer.toString(cal.get(Calendar.YEAR));
		beginMonth  = df.format(cal.get(Calendar.MONTH) + 1);
		beginDay   = df.format(cal.get(Calendar.DATE));
		beginDate = beginYear +"-"+ beginMonth +"-"+beginDay;

		return beginDate+","+endDate;
	}
}

데이터 베이스에 저장된 파일 이름과 파일 타입

Iterator 인터페이스의 구성은 아래와 같다.

Iterator는 자바의 컬렉션 프레임워크에서 컬렉션에 저장되어 있는 요소들을 읽어오는 방법을 표준화한 것이다.

컬렉션 프레임워크란 데이터를 저장하는 클래스들을 표준화한 설계이다.

public interface Iterator {
boolean hasNext();
Object next();
void remove();
}

Set, List, Map은 어떤 데이터들의 집합체라고 볼 수 있다.

Set과 List는 데이터의 그룹(Collection)이다.

Set은 순서를 유지하지 않는 데이터의 집합이다. 데이터의 중복이 허용되지않고 HashSet, TreSet 등이 있다.

List는 순서를 유지하는 데이터의 집합이다. 데이터의 중복이 허용되며, Vector, LinkedList, ArrayList 등이 있다.

 

Map은 키(Key)와 값(Value)으로 이루어진 데이터의 집합이다. 순서는 유지되지 않으며 키는 중복을 허용하지 않는다.

Map의 클래스로는 TreeMap, Hashtable, HashMap 등이 있다.

 

Interator는 이런 집합체로부터 정보를 얻어낸다고 볼 수 있다. 집합체를 다룰 때는 개별적인 클래스에 대해 데이터를 읽는 방법을 알아야 하기 때문에 각 컬렉션에 접근이 힘들어진다. Iterator를 쓰게 되면 어떤 컬렉션이라도 동일한 방식으로 접근이 가능하여 그 안에 있는 항목들에 접근할 수 있는 방법을 제공한다.(다형성)

 

Iterator의 메소드에는 hasNext(), next(), remove()가 있다.

각각의 기능은 다음과 같다.

 

  • hasNext() : 읽어올 요소가 남아있는지 확인하는 메소드, 요소가 있다면 true, 없다면 false
  • next() : 다음 데이터를 반환한다.
  • remove() : next()로 읽어온 요소를 삭제한다.

메소드의 호출 순서는 hasNext() -> next() -> remove이다.

 

아래 Iterator를 사용한 예제를 보도록하자.

import java.util.ArrayList;
import java.util.Iterator;

public class test {

	public static void main(String[] args) {
		System.out.println("List");

		ArrayList list = new ArrayList();

		/* 리스트에 다음과 같이 요일을 추가하였다. */
		list.add("토요일");
		list.add("일요일");
		list.add("월요일");

		/* Iterator 인터페이스에 다음과 같이 list.iterator를 저장하였다. */
		Iterator iter = list.iterator();

		/* hasNext로 객체가 있으면 true를 반환한다. 없으면 false */
		while(iter.hasNext()==true){
			/* day 변수에 iter 객체를 저장한다. 토,일,월 이렇게 저장하고 출력을 할 것이다. */
			String day = (String) iter.next();
			/* 월요일이 오는걸 원치않으니 다음과 같이 remove()로 삭제를 하자 */
			if(day == "월요일")
				iter.remove();
			System.out.println("요일 : "+day);
		}

		System.out.println("---------------------------------");

		iter = list.iterator();
		while(iter.hasNext()==true){
			String day = (String) iter.next();
			System.out.println("요일 : "+day);
		}

	}
}

참조 : http://blog.naver.com/PostView.nhn?blogId=qls0147&logNo=220771985885&categoryNo=0&parentCategoryNo=0&viewDate=¤tPage=1&postListTopCurrentPage=1&from=postView

 

 

+ Recent posts