上传头像 java_Java头像上传方法

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/zen4j/p/5571149.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值