SpringBoot+⼩程序上传头像SpringBoot
<!-- 引⼊swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!--集成通⽤mapper -->
<dependency>
<groupId&batis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<!-- apache ⼯具类 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
WebMvcConfig 配置
package fig;
import t.annotation.Configuration;
import org.springframework.fig.annotation.ResourceHandlerRegistry;
import org.springframework.fig.annotation.WebMvcConfigurerAdapter;
/**
* 虚拟⽬录配置
* 将服务⽬录映射成web资源
*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
//swagger增加url映射
registry.addResourceHandler("/**")
//swagger2静态资源⽂件配置
.addResourceLocations("classpath:/META-INF/resources/")
.addResourceLocations("classpath:/META-INF/resources/templates/")
.addResourceLocations("file:F:/weChatProject/movieProject/movieSystem/");
}
}
UserController
import java.io.File;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.sult.JSONResult;
import org.apachemons.io.IOUtils;
import org.apachemons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
@RestController
@Api(value="⽤户相关业务的接⼝", tags={"⽤户相关业务的controller"})
@RequestMapping("/user")
public class UserController extends BasicController {
@Autowired
private UserService userService;
//localhost:8080/swagger-ui.html
//http:localhost:8080/1002/face/a.png
/**
* ⽤户上传头像(单张)
* @param userId
* @param files
* @return
* @throws Exception
*/
@ApiOperation(value="⽤户上传头像", notes="⽤户上传头像的接⼝")
@ApiImplicitParam(name="userId", value="⽤户id", required=true,
dataType="String", paramType="query")
@PostMapping("/uploadFace")
public JSONResult uploadFace(String userId,@RequestParam("file") MultipartFile[] files)throws Exception { if(StringUtils.isBlank(userId)){
Msg("⽤户id不能为空...");
}
// ⽂件保存的命名空间
String fileSpace ="F:/WeChatProject/movieProject/movieSystem";
// 保存到数据库中的相对路径
String uploadPathDB ="/"+ userId +"/face";
FileOutputStream fileOutputStream = null;
InputStream inputStream = null;
try{
//如果端传来的头像不为空
if(files != null && files.length >0){
/
/获取⽂件名
String fileName = files[0].getOriginalFilename();
//如果⽂件名不为空
if(StringUtils.isNotBlank(fileName)){
// ⽂件上传的最终保存路径
String finalFacePath = fileSpace + uploadPathDB +"/"+ fileName;
// 设置数据库保存的路径
uploadPathDB +=("/"+ fileName);
File outFile =new File(finalFacePath);
//判断⽗⽂件夹是否存在
ParentFile()!= null ||!ParentFile().isDirectory()){
/
/ ⽗⽂件夹不存在则创建⽗⽂件夹
制作查询类小程序ParentFile().mkdirs();
}
fileOutputStream =new FileOutputStream(outFile);
inputStream = files[0].getInputStream();
}
}else{
Msg("上传出错...");
}
}catch(Exception e){
e.printStackTrace();
Msg("上传出错...");
}finally{
if(fileOutputStream != null){
fileOutputStream.flush();
fileOutputStream.close();
}
}
Users users=new Users();
users.setId(userId);
users.setFaceImage(uploadPathDB);
//修改⽤户信息
userService.updateUserInfo(users);
return JSONResult.ok(uploadPathDB);
}
xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.study.mapper.UsersMapper">
<resultMap id="BaseResultMap"type="del.Users">
<!--
WARNING - @ated
-->
<id column="id"property="id"jdbcType="VARCHAR"/>
<result column="username"property="username"jdbcType="VARCHAR"/>
<result column="password"property="password"jdbcType="VARCHAR"/>
<result column="face_image"property="faceImage"jdbcType="VARCHAR"/>
<result column="nickname"property="nickname"jdbcType="VARCHAR"/>
<result column="fans_counts"property="fansCounts"jdbcType="INTEGER"/>
<result column="follow_counts"property="followCounts"jdbcType="INTEGER"/>
<result column="receive_like_counts"property="receiveLikeCounts"jdbcType="INTEGER"/>
</resultMap>
</mapper>
MyMapper
package com.study.base.mapper;
batis.mappermon.Mapper;
batis.mappermon.MySqlMapper;
public interface MyMapper<T>extends Mapper<T>, MySqlMapper<T>{
//TODO
//FIXME 特别注意,该接⼝不能被扫描到,否则会出错
}
UserMapper
package com.study.mapper;
import com.study.base.mapper.MyMapper;
import del.Users;
public interface UsersMapper extends MyMapper<Users>{
}
SpringBoot主类
//batis.spring.annotation.MapperScan;
import t.annotation.ComponentScan;
batis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//扫描mapper⽂件
@MapperScan("com.study.mapper")
@ComponentScan(basePackages ={"com.study","fig"})
public class WechatMovieManageApplication {
public static void main(String[] args){
SpringApplication.run(WechatMovieManageApplication.class, args);
}
}
service
@Service
public class UserServiceImpl implements UserService {
@Resource
private UsersMapper userMapper;
@Transactional(propagation = Propagation.REQUIRED)
@Override
public void updateUserInfo(Users user){
Example userExample =new Example(Users.class);
Example.Criteria criteria = ateCriteria();
//通过ID查询⽤户
criteria.andEqualTo("id", Id());
//updateByExampleSelective是将⼀⾏中某⼏个属性更新,⽽不改变其他的值
userMapper.updateByExampleSelective(user, userExample);
}
}
JSONResult
package com.sult;
/**
* @Description: ⾃定义响应数据结构
*    这个类是提供给门户,ios,安卓,商城⽤的
*    门户接受此类数据后需要使⽤本类的⽅法转换成对于的数据类型格式(类,或者list) *    其他⾃⾏处理
*    200:表⽰成功
*    500:表⽰错误,错误信息在msg字段中
*    501:bean验证错误,不管多少个错误都以map形式返回
*    502:拦截到⽤户token出错
*    555:异常抛出信息
*/
public class JSONResult {
// 响应业务状态
private Integer status;
// 响应消息
private String msg;
// 响应中的数据
private Object data;
private String ok;// 不使⽤
public static JSONResult build(Integer status, String msg, Object data){ return new JSONResult(status, msg, data);
}
public static JSONResult ok(Object data){
return new JSONResult(data);
}
public static JSONResult ok(){
return new JSONResult(null);
}
public static JSONResult errorMsg(String msg){
return new JSONResult(500, msg, null);
}
public static JSONResult errorMap(Object data){
return new JSONResult(501,"error", data);
}
public static JSONResult errorTokenMsg(String msg){
return new JSONResult(502, msg, null);
}
public static JSONResult errorException(String msg){
return new JSONResult(555, msg, null);
}
public JSONResult(){
}
public JSONResult(Integer status, String msg, Object data){
this.status = status;
this.msg = msg;
this.data = data;
}
public JSONResult(Object data){
this.status =200;
this.msg ="OK";
this.data = data;
}
public Boolean isOK(){
return this.status ==200;
}
public Integer getStatus(){
return status;
}
public void setStatus(Integer status){
this.status = status;
}
public String getMsg(){
return msg;
}
public void setMsg(String msg){
this.msg = msg;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。