Java 如何使用SpringMVC和jsp显示mysql数据库中的@Lob图像

Java 如何使用SpringMVC和jsp显示mysql数据库中的@Lob图像,java,mysql,spring-mvc,jsp,blob,Java,Mysql,Spring Mvc,Jsp,Blob,有到github的链接:我使用的是Spring4.3.7.RELEASE、MySQL连接器Java:5.1.39和hibrnate:5.2.9。菲纳利 我有一个用户和他的帐户模型。在帐户中,我有@Lob accPicture和一些字符串(+get/set)。我尝试了stackoverflow和文档中的许多答案来显示帐户图像,但没有成功。最后想想我所做的:创建自己的ImageController。我成功地将图像存储在数据库中,但当我试图在jsp中显示它时,它显示为“HTTP状态400”- 客户端发

有到github的链接:我使用的是Spring4.3.7.RELEASE、MySQL连接器Java:5.1.39和hibrnate:5.2.9。菲纳利 我有一个用户和他的帐户模型。在帐户中,我有@Lob accPicture和一些字符串(+get/set)。我尝试了stackoverflow和文档中的许多答案来显示帐户图像,但没有成功。最后想想我所做的:创建自己的ImageController。我成功地将图像存储在数据库中,但当我试图在jsp中显示它时,它显示为“HTTP状态400”- 客户端发送的请求在语法上不正确。” 首先,我向您展示我的用户模型:

@Entity
@Table(name = "users")
public class User implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(unique = true)
    @NotBlank
    @Size(min=4,max=20)
    private String login;

    @NotBlank
    private String password;

    @Column(unique = true)
    @Email
    @NotBlank
    private String email;

    private String permission;

    @OneToMany()
    private List<Account> accounts;


    public User(final String login, final String password, final String email) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(login));
        Preconditions.checkArgument(!Strings.isNullOrEmpty(password));
        Preconditions.checkArgument(!Strings.isNullOrEmpty(email));
        this.login = login;
        this.password = password;
        this.email = email;
    }

    public User() {
    }
}
+ get/set
下一个客户控制员:

@Controller
public class AccountController {

    @Autowired
    AccountRepository accountRepository;

    @Autowired
    UserRepository userRepository;


    @RequestMapping(method = RequestMethod.GET, value ="addAccount")
    public String addAccount(Model model) {
        Account account = new Account();
        model.addAttribute("account", account);

        return "addAccount";
    }

    @RequestMapping(method = RequestMethod.POST, value ="addAccount")
    public String addAccount(@ModelAttribute Account account, HttpSession session) {
        User user = userRepository.findOne((Long) session.getAttribute("user_id"));
        account.setIfBasicAccount(false);
        account.setUser(user);
        accountRepository.save(account);
        return "redirect:/accounts";
    }

    @RequestMapping("/accounts")
    public String accountList(Model model, HttpSession ses) {
        long userId = (Long) ses.getAttribute("user_id");
        List<Account> accounts = accountRepository.findUserAccounts(userId);
        model.addAttribute("accounts", accounts);
        return "accounts";
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String editAccountForm(Model model, @PathVariable long id) {
        Account account = accountRepository.findOne(id);
        model.addAttribute("account",account);
        return "editAccountForm";
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
    public String editAccount(@ModelAttribute Account account, @PathVariable long id) {
        Account accountToUpdate = accountRepository.findOne(id);
        accountToUpdate.setAccTitle(account.getAccTitle());
        accountToUpdate.setAccFirstName(account.getAccFirstName());
        accountToUpdate.setAccLastName(account.getAccLastName());
        accountToUpdate.setAccBirthdate(account.getAccBirthdate());
        accountToUpdate.setAccPhoneNumber(account.getAccPhoneNumber());
        accountToUpdate.setAccEducation(account.getAccEducation());
        accountToUpdate.setAccExperience(account.getAccExperience());
        accountToUpdate.setAccAbilities(account.getAccAbilities());
        accountToUpdate.setAccInterests(account.getAccInterests());
        accountToUpdate.setAccProjects(account.getAccProjects());
        accountToUpdate.setAccDescription(account.getAccDescription());
        accountRepository.save(accountToUpdate);
        return "redirect:/accounts";
    }

    @RequestMapping("/delete")
    public String deleteAccount(Model model) {
        return "deleteAccount";
    }

    @RequestMapping("/read/{id}")
    public String read(@PathVariable long id) {
        return accountRepository.findOne(id).toString();
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable long id) {
        Account account = accountRepository.findOne(id);
        accountRepository.delete(account);
        return "redirect:/accounts";
    }
}
@Controller
@RequestMapping("/admin/user")
public class ImageController {

    @Autowired
    AccountRepository accountRepository;

    @RequestMapping(value = "/accounts", method = RequestMethod.GET)
    public void showImage(@RequestParam("id") long id, Model model) {

        Account account = accountRepository.findById(id);
        String photoencodeBase64 = account.getStringPhoto();
        model.addAttribute("accPicture", photoencodeBase64);

    }
}
要显示帐户的my.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
     <%@ page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ include file="/WEB-INF/parts/header.jsp" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<table class="table table-striped">
<h1>Accounts:</h1>
<c:forEach items="${accounts}" var="account" begin="0" varStatus="theCount">

    <tr>
        <td>${theCount.index+1}</td>
        <td><b>Nazwa: </b>${account.accTitle}</td>
        <td><b>Opis: </b>${account.accDescription}</td>
        <td><img src="/ProjectProfile/user/accounts?id=${account.id}"/></td>
        <td><a style="width: 180px;height: 20px;" href="./edit/${account.id}" class="badge badge-primary">Show/Edit</a></td>
        <td><a style="width: 180px;height: 20px;" href="./delete/${account.id}" class="badge badge-danger">Delete</a></td>
    </tr>
</c:forEach>
</table>

     <a href="<c:url value="/addAccount"/>">Add Account</a>

</body>
</html>

在此处插入标题
账户:
${count.index+1}
Nazwa:${account.accTitle}
Opis:${account.accscription}
也许我需要使用Base64编码器,但我不知道如何使用。。。。我使用pom.xml和AppConfig进行配置。拜托,看看这个项目,也许有人能帮上忙

<img id="photo" src="data:image/png;base64,${PHOTOYOUNEED}" />
我还在模型中使用此方法将byte[]转换为base64中的字符串:

public static String convertBinImageToString(byte[] binImage) {
    if(binImage!=null && binImage.length>0) {
        return Base64.getEncoder().encodeToString(binImage);
    }
    else
        return "";
}

我在模型内部的getStringPhoto()getter中调用它。

好的Eunito,让我们看看。。。已更改的Account.java(模型):

我有两个控制器(一个仅用于显示图像-我不太确定这是件好事,因为我有两个相同的RequestMapping)。因此,请参见更改的ImageController:

@Controller
public class AccountController {

    @Autowired
    AccountRepository accountRepository;

    @Autowired
    UserRepository userRepository;


    @RequestMapping(method = RequestMethod.GET, value ="addAccount")
    public String addAccount(Model model) {
        Account account = new Account();
        model.addAttribute("account", account);

        return "addAccount";
    }

    @RequestMapping(method = RequestMethod.POST, value ="addAccount")
    public String addAccount(@ModelAttribute Account account, HttpSession session) {
        User user = userRepository.findOne((Long) session.getAttribute("user_id"));
        account.setIfBasicAccount(false);
        account.setUser(user);
        accountRepository.save(account);
        return "redirect:/accounts";
    }

    @RequestMapping("/accounts")
    public String accountList(Model model, HttpSession ses) {
        long userId = (Long) ses.getAttribute("user_id");
        List<Account> accounts = accountRepository.findUserAccounts(userId);
        model.addAttribute("accounts", accounts);
        return "accounts";
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String editAccountForm(Model model, @PathVariable long id) {
        Account account = accountRepository.findOne(id);
        model.addAttribute("account",account);
        return "editAccountForm";
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
    public String editAccount(@ModelAttribute Account account, @PathVariable long id) {
        Account accountToUpdate = accountRepository.findOne(id);
        accountToUpdate.setAccTitle(account.getAccTitle());
        accountToUpdate.setAccFirstName(account.getAccFirstName());
        accountToUpdate.setAccLastName(account.getAccLastName());
        accountToUpdate.setAccBirthdate(account.getAccBirthdate());
        accountToUpdate.setAccPhoneNumber(account.getAccPhoneNumber());
        accountToUpdate.setAccEducation(account.getAccEducation());
        accountToUpdate.setAccExperience(account.getAccExperience());
        accountToUpdate.setAccAbilities(account.getAccAbilities());
        accountToUpdate.setAccInterests(account.getAccInterests());
        accountToUpdate.setAccProjects(account.getAccProjects());
        accountToUpdate.setAccDescription(account.getAccDescription());
        accountRepository.save(accountToUpdate);
        return "redirect:/accounts";
    }

    @RequestMapping("/delete")
    public String deleteAccount(Model model) {
        return "deleteAccount";
    }

    @RequestMapping("/read/{id}")
    public String read(@PathVariable long id) {
        return accountRepository.findOne(id).toString();
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable long id) {
        Account account = accountRepository.findOne(id);
        accountRepository.delete(account);
        return "redirect:/accounts";
    }
}
@Controller
@RequestMapping("/admin/user")
public class ImageController {

    @Autowired
    AccountRepository accountRepository;

    @RequestMapping(value = "/accounts", method = RequestMethod.GET)
    public void showImage(@RequestParam("id") long id, Model model) {

        Account account = accountRepository.findById(id);
        String photoencodeBase64 = account.getStringPhoto();
        model.addAttribute("accPicture", photoencodeBase64);

    }
}
和.jsp来显示图像:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
     <%@ page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ include file="/WEB-INF/parts/header.jsp" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<table class="table table-striped">
<h1>Accounts:</h1>
<c:forEach items="${accounts}" var="account" begin="0" varStatus="theCount">

    <tr>
        <td>${theCount.index+1}</td>
        <td><b>Title: </b>${account.accTitle}</td>
        <td><b>Description: </b>${account.accDescription}</td>
        <td><b>Image: </b><img id="photo" src="data:image/png;base64,${account.accPicture}" /></td>
        <td><a style="width: 180px;height: 20px;" href="./edit/${account.id}" class="badge badge-primary">Show/Edit</a></td>
        <td><a style="width: 180px;height: 20px;" href="./delete/${account.id}" class="badge badge-danger">Delete</a></td>
    </tr>
</c:forEach>
</table>

     <a href="<c:url value="/addAccount"/>">Add Account</a>

</body>
</html>

在此处插入标题
账户:
${count.index+1}
标题:${account.accTitle}
说明:${account.accDescription}
图片:
因此,当我添加新帐户->写入标题、名称等,并从我的浏览器显示的文件中添加图像时,会发生什么?HTTP状态400?客户端发送的请求在语法上不正确。->我需要查看所有用户帐户。 在STS控制台中,什么都没有发生。在MySQL中也是如此。

为什么不使用?这可以提供存储服务和rest端点,用于管理与jpa实体关联的内容

pom.xml

要关联内容,请将Spring内容批注添加到您的帐户实体

Account.java

创建“存储”:

AccountImagesStore.java

@StoreRestResource(path=“accountImages)
公共接口AccountImagesStore扩展了ContentStore。参考指南是。还有一个教程视频。编码位大约从1/2开始


HTH

当询问异常时,始终发布异常的准确完整堆栈跟踪。同时告诉我们是哪个请求导致了此异常的发生。不相关,但图像有一种内容类型,而不是4种不同的内容类型。好的,现在我看到NullPointerException:SEVERE:Servlet.service()for Servlet[dispatcher]在具有路径的上下文中[/ProjectProfile]在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)的pl.lukszn.ProjectProfile.controllers.ImageController.showImage(ImageController.java:27)处引发了异常[请求处理失败;嵌套异常为java.lang.NullPointerException],根本原因为java.lang.NullPointerException]那么,看看ImageController的第27行有什么可以为空。我们这里没有行号。Account=accountRepository.getOne(id);因此accountRepository为null。这很正常,因为您没有使用构造函数或自动连线批注正确地注入它。没有任何更改,可能是我做错了什么…添加到帐户模型:private String stringPhoto;和他的getter:public String getStringPhoto(){return convertBinImageToString(acccpicture);},下一个更改的ImageController:@RequestMapping(value=“/accounts”,method=RequestMethod.GET)public void showImage(@RequestParam(“id”)长id,模型模型){Account Account=accountRepository.findById(id);String photoencodeBase64=Account.getStringPhoto();Model.addAttribute(“acccpicture”,photoencodeBase64);}…和jsp…base64,${account.accImage}”/>发布时,您可以使用“代码示例{}”获取模型、控制器和jsp的更改主代码吗?另外,错误消息是什么?您也可以使用“代码示例{}”将其放置在此处吗?谢谢!:我为你的评论添加了新答案,很高兴它有所帮助。Spring内容还有很多其他功能,如格式副本和全文搜索。不知道它们是否有用,但请以防万一。
@Controller
@RequestMapping("/admin/user")
public class ImageController {

    @Autowired
    AccountRepository accountRepository;

    @RequestMapping(value = "/accounts", method = RequestMethod.GET)
    public void showImage(@RequestParam("id") long id, Model model) {

        Account account = accountRepository.findById(id);
        String photoencodeBase64 = account.getStringPhoto();
        model.addAttribute("accPicture", photoencodeBase64);

    }
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
     <%@ page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ include file="/WEB-INF/parts/header.jsp" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<table class="table table-striped">
<h1>Accounts:</h1>
<c:forEach items="${accounts}" var="account" begin="0" varStatus="theCount">

    <tr>
        <td>${theCount.index+1}</td>
        <td><b>Title: </b>${account.accTitle}</td>
        <td><b>Description: </b>${account.accDescription}</td>
        <td><b>Image: </b><img id="photo" src="data:image/png;base64,${account.accPicture}" /></td>
        <td><a style="width: 180px;height: 20px;" href="./edit/${account.id}" class="badge badge-primary">Show/Edit</a></td>
        <td><a style="width: 180px;height: 20px;" href="./delete/${account.id}" class="badge badge-danger">Delete</a></td>
    </tr>
</c:forEach>
</table>

     <a href="<c:url value="/addAccount"/>">Add Account</a>

</body>
</html>
   <!-- Java API -->
   <dependency>
      <groupId>com.github.paulcwarren</groupId>
      <artifactId>spring-content-jpa</artifactId>
      <version>0.1.0</version>
   </dependency>
   <!-- REST API -->
   <dependency>
      <groupId>com.github.paulcwarren</groupId>
      <artifactId>spring-content-rest</artifactId>
      <version>0.1.0</version>
   </dependency>
@Configuration
@EnableJpaStores
@Import("org.springframework.content.rest.config.RestConfiguration.class")
public class MysqlConfig {

    // schema management
    // 
    @Value("/org/springframework/content/jpa/schema-drop-mysql.sql")
    private Resource dropRepositoryTables;

    @Value("/org/springframework/content/jpa/schema-mysql.sql")
    private Resource dataRepositorySchema;

    @Bean
    DataSourceInitializer datasourceInitializer() {
        ResourceDatabasePopulator databasePopulator =
                new ResourceDatabasePopulator();

        databasePopulator.addScript(dropReopsitoryTables);
        databasePopulator.addScript(dataReopsitorySchema);
        databasePopulator.setIgnoreFailedDrops(true);

        DataSourceInitializer initializer = new DataSourceInitializer();
        initializer.setDataSource(dataSource());
        initializer.setDatabasePopulator(databasePopulator);

        return initializer;
    }
}
@Entity
public class Account {

   // replace @Lob field with

   @ContentId
   private String contentId;

   @ContentLength
   private long contentLength = 0L;

   // if you have rest endpoints
   @MimeType
   private String mimeType = "text/plain";
@StoreRestResource(path="accountImages)
public interface AccountImagesStore extends ContentStore<Account, String> {
}