Last active
August 29, 2015 14:05
-
-
Save yim1990/986f7f0e1256fd8e747a to your computer and use it in GitHub Desktop.
Multipart file upload class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.image.BufferedImage; | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.InputStream; | |
import javax.imageio.ImageIO; | |
import org.apache.commons.codec.digest.DigestUtils; | |
import org.apache.commons.io.FilenameUtils; | |
import org.apache.commons.io.IOUtils; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.core.env.Environment; | |
import org.springframework.core.io.Resource; | |
import org.springframework.stereotype.Service; | |
import org.springframework.web.multipart.MultipartFile; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
@Service | |
public class UploadServiceImpl implements UploadService { | |
private static final Logger logger = LoggerFactory.getLogger(UploadServiceImpl.class); | |
@Autowired private ObjectMapper mapper; | |
@Autowired private Environment environment; | |
@Autowired private ApplicationContext applicationContext; | |
@Value("${upload.path}") private String uploadPath; | |
@Value("${upload.url}") private String uploadUrl; | |
public class UploadedFile { | |
private String url; | |
private String hash; | |
private Integer width; | |
private Integer height; | |
public UploadedFile(String url, String hash, Integer width, Integer height) { | |
this.url=url; | |
this.hash=hash; | |
this.width=width; | |
this.height=height; | |
} | |
public String getHash() { | |
return hash; | |
} | |
public String getUrl() { | |
return url; | |
} | |
public Integer getHeight() { | |
return height; | |
} | |
public Integer getWidth() { | |
return width; | |
} | |
} | |
@Override | |
public UploadedFile uploadImageFile(MultipartFile multipartFile) { | |
String extension=FilenameUtils.getExtension(multipartFile.getOriginalFilename()); | |
BufferedOutputStream bos=null; | |
InputStream is=null; | |
try { | |
File directory=new File(uploadPath); | |
if(!directory.exists() && !directory.mkdirs()) { //경로 없으면 생성 | |
return null; | |
} | |
String hash=DigestUtils.md5Hex(multipartFile.getBytes()); | |
Resource outputResource=applicationContext.getResource("file:"+uploadPath+"/"+hash+"."+extension); | |
File outputFile=outputResource.getFile(); | |
BufferedImage bImg=ImageIO.read(multipartFile.getInputStream()); //ImageIO는 stream을 닫아버려서 stream 재활용 불가. | |
Integer width=bImg.getWidth(); | |
Integer height=bImg.getHeight(); | |
is=multipartFile.getInputStream(); | |
bos=new BufferedOutputStream(new FileOutputStream(outputFile)); | |
IOUtils.copy(is, bos); | |
return new UploadedFile(uploadUrl+"/"+outputFile.getName(), hash, width, height); | |
} catch(Exception e) { | |
logger.error("Error on upload image", e); | |
return null; | |
} finally { | |
IOUtils.closeQuietly(bos); | |
IOUtils.closeQuietly(is); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment