关于上传图片和显示

转载:https://blog.csdn.net/aslongasyoulike/article/details/77001824?locationNum=9&fps=1

注册界面:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
<form action="/zhuce" th:action="@{/zhuce}" method="post" enctype="multipart/form-data" >
    <label>姓名</label><input type="text" name="name"/>
    <label>密码</label><input type="password" name="password"/>
    <label>上传图片</label>
    <input type="file" name="file"/>
    <input type="submit" value="上传"/>
</form>
</body>
</html>

control如下:

package com.example.demo.control;

import com.example.demo.dao.UserRepository;
import com.example.demo.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;


@Controller
public class Control {
    @Autowired
    UserRepository userRepository;
    @GetMapping(value="/zhuce")
    public String zhuce(){
        return "zhuce";
    }
    @PostMapping(value="/zhuce")
    public String tijiao(@RequestParam(value="name") String name,
                         @RequestParam(value="password") String password,
                         @RequestParam(value="file")MultipartFile file,
                         Model model) {
        User user = new User();
        user.setUsername(name);
        user.setPassword(password);
        if (!file.isEmpty()) {
            try {
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File("f:\旗杯\demo5\src\main\webapp\"+name+".jpg")));//保存图片到目录下
                out.write(file.getBytes());
                out.flush();
                out.close();
                String filename="f:\旗杯\demo5\src\main\webapp\"+name+".jpg";
                user.setTupian(filename);
                userRepository.save(user);//增加用户
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            }
            model.addAttribute(user);
            return "permanager";
        } else {
            return "上传失败,因为文件是空的.";
        }
    }
}

个人中心:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
<p>用户名:</p>
<p th:text="${user.username}"></p>
<p>图片:</p>
<img th:src="@{${user.username}+'.jpg'}"/>
</body>
</html>

对webapp路径的配置

package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter{
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/src/main/webapp/**").addResourceLocations("classpath:/webapp/");
        super.addResourceHandlers(registry);
    }
}

对应的用户实体类:

package com.example.demo.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * Created by 18274 on 2017/8/9.
 */
@Entity
public class User {
    @Id
    @GeneratedValue
    private Long id;
    private String username;
    private String password;
    private String tupian;//图片地址
    public User(){}

    public Long getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getTupian() {
        return tupian;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setTupian(String tupian) {
        this.tupian = tupian;
    }
}

用户实体类的接口:

package com.example.demo.dao;

import com.example.demo.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;


public interface UserRepository extends JpaRepository<User,Long>{
}
原文地址:https://www.cnblogs.com/llfy/p/9100852.html