본문 바로가기

spring

파일업로드 클래스

package com.example.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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 FileController {
	@Resource(name="uploadPath")
	private String path;
	
	//파일업로드 메소드
	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("/display")
	   public byte[] display(String fileName)throws Exception{
	      InputStream in=new FileInputStream(path + "/" + fileName);
	      byte[] image=IOUtils.toByteArray(in);
	      in.close();
	      return image;
	   }
	
	
	@ResponseBody
	@RequestMapping(value="upload",method=RequestMethod.POST)
	public String upload(MultipartFile file)throws Exception{
		System.out.println("file:" + file.getOriginalFilename());
		
		String savedName=uploadFile(file.getOriginalFilename(),file.getBytes());
		return savedName;
	}
	
	//파일삭제
	@ResponseBody
	@RequestMapping(value="deleteFile",method=RequestMethod.POST)
	public void delete(String fileName){
		new File(path +"/" +fileName).delete();
	}
	//파일 다운로드
	@ResponseBody
	  @RequestMapping(value="downloadFile")
	  public ResponseEntity<byte[]> downloadFile(String fileName)throws Exception{
	    System.out.println("파일이름:" + fileName);
	    ResponseEntity<byte[]> entity=null;
	    InputStream in = null;
	    try{
	      HttpHeaders headers = new HttpHeaders();
	      in= new FileInputStream(path+ "/" + fileName);
	      fileName = fileName.substring(fileName.indexOf("_") + 1);
	      headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
	      headers.add("Content-Disposition", "attachment;filename=\"" + 
	                            new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + "\"");
	      entity = new ResponseEntity<byte[]>(IOUtils.toByteArray(in), headers, HttpStatus.CREATED);
	    }catch(Exception e){
	      System.out.println(e.toString());
	      entity= new ResponseEntity<byte[]>(HttpStatus.BAD_REQUEST);
	    }finally{
	      in.close();
	    }
	    return entity;
	  }
	
}

'spring' 카테고리의 다른 글

프로필 사진 수정하기  (0) 2019.12.06
파일 업로드  (0) 2019.12.05
파일업로드  (0) 2019.11.29
파일업로드  (0) 2019.11.29
게시판에 댓글을 달면 게시글의 댓글수가 추가됩니다.  (0) 2019.11.28