简体   繁体   中英

Check if item already exists in DB

I am new to Java and Spring Framework, and I'm learning it by a file upload example. I have a controller for uploading of a file. Before I save anything to DB, I would like to check if the file with the same name already exists and return an error message for it. This is how the controller looks like:

@PostMapping("/file-upload")
public String postFile(@RequestParam("fileUpload") MultipartFile fileUpload, Authentication authentication, Model model) throws IOException {
    if(fileUpload.isEmpty()) {
        model.addAttribute("success",false);
        model.addAttribute("message","No file selected to upload!");
        return "home";
    }

    if(fileService.getByFilename(fileUpload.getOriginalFilename()) != null) {
        model.addAttribute("success",false);
        model.addAttribute("message","File with that name already exists");
        return "home";
    }

    User user = this.userService.getUser(authentication.getName());
    Integer userId = user.getUserId();
    fileService.createFile(fileUpload, userId);
    model.addAttribute("success",true);
    model.addAttribute("message","New File added successfully!");
    return "home";
}

And this is the fileService getByFilename method:

public File getByUsername(String filename) {
    return fileMapper.getByUsername(filename);
}

And finally fileMapper getByFilename :

@Select("SELECT * FROM FILES WHERE filename = #{filename}")
File getByUsername(String filename);

But, when I try to upload a file like that, I get an error:

There was an unexpected error (type=Internal Server Error, status=500). Error attempting to get column 'FILEDATA' from result set. Cause: org.h2.jdbc.JdbcSQLDataException: Data conversion error converting

This is how the table looks like:

CREATE TABLE IF NOT EXISTS FILES (
    fileId INT PRIMARY KEY auto_increment,
    filename VARCHAR,
    contenttype VARCHAR,
    filesize VARCHAR,
    userid INT,
    filedata BLOB,
    foreign key (userid) references USERS(userid)
);

How should I fix this? Here is the link to the repo .

This is the File class:

package com.udacity.jwdnd.course1.cloudstorage.model;

public class File {
    private Integer fileId;
    private String filename;
    private String contenttype;
    private Long filesize;
    private byte[] filedata;
    private Integer userid;

    public File(Integer fileId, String filename, String contenttype, Long filesize, byte[] filedata, Integer userid) {
        this.fileId = fileId;
        this.filename = filename;
        this.contenttype = contenttype;
        this.filesize = filesize;
        this.filedata = filedata;
        this.userid = userid;
    }

    public Integer getFileId() {
        return fileId;
    }

    public void setFileId(Integer fileId) {
        this.fileId = fileId;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getContenttype() {
        return contenttype;
    }

    public void setContenttype(String contenttype) {
        this.contenttype = contenttype;
    }

    public Long getFilesize() {
        return filesize;
    }

    public void setFilesize(Long filesize) {
        this.filesize = filesize;
    }

    public byte[] getFiledata() {
        return filedata;
    }

    public void setFiledata(byte[] filedata) {
        this.filedata = filedata;
    }

    public Integer getUserid() {
        return userid;
    }

    public void setUserid(Integer userid) { this.userid = userid; }
}

The structure of your table strongly suggests that a single row contains all the data: It does not represent a file at all, and there is no file on your file system : There is a bunch of binary data in the filedata column.

It is not possible to just treat this as a java.io.File object, which is a very light wrapper around a physical file on your file system (really - just check it out, File has one field, of type String, containing a path. That's all it has).

Therefore you just can't do what you want here. You need to find an alternative:

Save the data to an actual file

This involves having some sort of store on the disk, perhaps a tmp dir, where you explicitly write out all the blob data. You'll need to worry about clearing this stuff out or you're going to grow endless files in there.

Who cares about files?

Why do you NEED a file object? You should build software to be as abstract as seems reasonable at the time, and 'I need a file' is rarely the right level. The right level is usually either 'byte array' or 'InputStream' or something similar. Fix your program's flow so that they need only a byte array or better yet an inputstream and use JDBC's blob functionality to take care of this (I don't actually know how to unlock this using spring; you may have to write some code instead of just an @Select annotated method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM