码迷,mamicode.com
首页 > 编程语言 > 详细

[Utils]Java头像上传方法

时间:2016-05-31 13:57:37      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 *头像上传工具类
 * Created by Zenz.
 */
public class Upload {
    /**
     * 头像上传
     * @param headImage 头像上传文件
     * @param request HTTP请求
     * @param dirs 保存目录
     * @throws IOException IO异常
     * @return 文件名称称
     */
    public static String UploadImg (MultipartFile headImage, HttpServletRequest request,
       String dirs) throws IOException {
        //1.保存头像
        //1.1 设置路径
        //1.1。1 获取保存绝对路径(upload/user目录下)
        String filePath = request.getSession().getServletContext().getRealPath(dirs);
        //1.1.2 设置头像名称
        String fileName =UUIDUtil.getUUID() + headImage.getOriginalFilename();
        //1.2复制文件
        //1.2.1 创建文件
        File targetFile = new File(filePath, fileName);
        //1.2.2 写入硬盘
        targetFile.mkdirs();
        //1.2.3复制头像
        headImage.transferTo(targetFile);
        return fileName;
    }
}

  

[Utils]Java头像上传方法

标签:

原文地址:http://www.cnblogs.com/zenzzat/p/5545485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!