标签:
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 获取保存绝对路径(upload/user目录下) String filePath = request.getSession().getServletContext().getRealPath(dirs); //1.1.2 设置头像名称 String fileName =UUIDUtil.getUUID() + headImage.getOriginalFilename(); System.out.println(fileName); //1.2复制文件 //1.2.1 创建文件 File targetFile = new File(filePath, fileName); //1.2.2 写入硬盘 targetFile.mkdirs(); //1.2.3复制头像 headImage.transferTo(targetFile); //返回带路径的头像名称 fileName = "/"+dirs+"/"+fileName; return fileName; } }
标签:
原文地址:http://www.cnblogs.com/zenzzat/p/5558494.html