Spring Fileverwaltung in Datenbank oder Logfile?

Bitte aktiviere JavaScript!
Ich habe eine Springablikation, die auch Dateien hochladen können soll. Dazu habe ich mir gedacht verwaltet ich die Files in einer eignen Enität, aber irgendwie speichern die nicht richtig in die Datenbank.

Der Relevante Code ist glaube:
Java:
package at.qe.sepm.skeleton.model;

import javax.persistence.Entity;

/**
 * Users can upload files to use in Questions and or answers.
 * @author eclipseworker
 */

import org.springframework.data.domain.Persistable;

import java.util.Date;
import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity(name="UserFile")
public class UserFile implements Persistable<Long>  {


    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "userFileId", updatable = false, nullable = false)
    private Long id;
    
    @ManyToOne
        @JoinColumn
       private User owner;
    
    private String fileName;
    
    
    
    @Column(nullable = false)
    private String path;
    
    //...
    
}
Im User ist es ähnlich:
Java:
    @OneToMany(cascade = {CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH}, mappedBy = "owner")
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<UserFile> files;
Die Uploadmethode sollte das USerfile auch speichern:

Java:
public void upload() throws Exception {
        //System.err.println("Starting upload.");
        
        if(file != null) {
            String filename = FilenameUtils.getName(file.getFileName());
            File dircetory = new File(path);
            if(!dircetory.isDirectory()) {
                dircetory.mkdir();
            }
            File outFile=new File(dircetory, filename);
            boolean outFileExists=outFile.exists();
            if(outFileExists && !overwrite) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "File exists!", file.getFileName() + " already exist. Rename it or overwrite it.");
                FacesContext.getCurrentInstance().addMessage(null, message);
            }else {
                try (
                    InputStream input = file.getInputstream();
                    OutputStream output = new FileOutputStream(outFile);
                ){
                    IOUtils.copy(input, output);
                }
                UserFile userFile;
                Timestamp now=new Timestamp(System.currentTimeMillis());
                if(outFileExists) {
                    Collection <UserFile> ufl=userFileService.findByFileNameAndOwner(outFile.getName(), owner);
                    userFile=ufl.iterator().next();
                }else {
                    userFile=new UserFile();
                    userFile.setOwner(owner);
                    userFile.setFileName(outFile.getName());
                    userFile.setSize(file.getSize());
                    userFile.setIntern(true);
                    userFile.setPath(path);
                    userFile.setUploadTime(now);
                }
                userFile.setLastUpdated(now);
                userFile.setVisible(visible);
                userFileService.saveUserFile(userFile);
                FacesMessage message = new FacesMessage("Succesful!", file.getFileName() + " is uploaded.");
                FacesContext.getCurrentInstance().addMessage(null, message);
            }
        }else {
            FacesMessage message = new FacesMessage("File null!!");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }
Der Fileservice über gibt nur alles an die Respotrory die so wie sie ist stimmt.
Aber trotzdem wird das UserFile nicht gespeichert. Oder zumindest im FileTable nicht angezeigt:
HTML:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<ui:composition xmlns="http://www.w3c.org/1999/xhtml"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                xmlns:pe="http://primefaces.org/ui/extensions"
                template="/WEB-INF/templates/main.xhtml">
    <ui:define name ="content">

        <h:form id="fileForm">
              
              
          
              
               <p:button href="fileupload.xhtml" value="Create File" />
              
              <p:dataTable id="userFileTable" var="file" value="#{userFileController.findAllMyFiles}" style="width:1200px;" emptyMessage="No files found.">
                  <f:facet name="header">
                    My files
                </f:facet>
                  
                <p:column headerText="File Name" filterBy="#{file.fileName}" filterMatchMode="contains" style="text-align:center;">
                     <h:outputText value="#{file.fileName}"/>
                </p:column>
                <p:column headerText="Owner" filterBy="#{file.owner.username}" filterMatchMode="contains" style="text-align:center;">
                     <h:outputText value="#{file.owner.username}"/>
                </p:column>
                <p:column headerText="Path" filterBy="#{file.path}" filterMatchMode="contains" style="text-align:center;">
                    <h:outputText value="#{file.path}"/>
                </p:column>
                <p:column headerText="Upload Time" filterBy="#{file.uploadTime}" filterMatchMode="contains" style="text-align:center;">
                             <h:outputText value="#{file.uploadTime}"/>
                 </p:column>
                <p:column headerText="Last Updated" filterBy="#{file.lastUpdated}" filterMatchMode="contains" style="text-align:center;">
                    <h:outputText value="#{file.lastUpdated}"/>
                 </p:column>
                <p:column headerText="Size" filterBy="#{file.size}" filterMatchMode="contains" style="text-align:center;">
                             <h:outputText value="#{file.size}"/>
                  </p:column>
                
                <p:column headerText="Visible" filterBy="#{file.visible}" filterMatchMode="contains" style="text-align:center;">
                             <p:selectBooleanCheckbox  disabled="true" value="#{file.visible}"/>
                </p:column>
                <p:column headerText="Intern" filterBy="#{file.intern}" filterMatchMode="contains" style="text-align:center;">
                             <p:selectBooleanCheckbox  disabled="true" value="#{file.intern}"/>
                </p:column>
                
                
                <p:column style="text-align:center" exportable="false">
                    <p:commandButton oncomplete="PF('editFileDialog').show()" icon="fa fa-pencil" update=":userFileTable:editFileDialog" title="Edit File (Link to new version)" >
                        <f:setPropertyActionListener  value="#{file}" target="#{userFileController.file}"/>
                    </p:commandButton>
                    <p:commandButton action="#{userFileController.doDeleteFile()}" icon="fa fa-trash" title="Delete" update=":userForm:notesTable">
                        <f:setPropertyActionListener value="#{file}" target="#{userFileController.note}" />
                        <p:confirm header="Confirmation" message="Do you really want to delete this file?" icon="ui-icon-alert" />
                    </p:commandButton>
                </p:column>
              
              </p:dataTable>
              
              
              <p:contextMenu for="userFileTable" widgetVar="userFileTable">   
               <p:menuitem value="Edit Cell" icon="ui-icon-search" /> 
               <p:menuitem value="Hide Menu" icon="ui-icon-close" /> 
           </p:contextMenu>
              
              
              <p:confirmDialog global="true" showEffect="fade" hideEffect="fade" width="300">
                <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
                <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
            </p:confirmDialog>
            
              <p:dialog id="editFileDialog" widgetVar="editFileDialog" header="Edit File" resizable="false" modal="true" showEffect="fade" hideEffect="fade" >
                
                <p:column headerText="FileName" filterBy="#{file.fileName}" filterMatchMode="contains" style="text-align:center;">
                     <p:inputText value="#{file.fileName}"/>
                </p:column>
                
                <p:column id="path"  headerText="Path" filterBy="#{file.path}" filterMatchMode="contains" style="text-align:center;">
                    <p:inputText value="#{file.path}"/>
                </p:column>
                <p:selectBooleanButton id="replacementStrategy" value="#{userFileController.replacePerUpload}" onLabel="Replace per upload" offLabel="Replace per link" style="width:60px" />

                <p:column headerText="Visible" filterBy="#{file.visible}" filterMatchMode="contains" style="text-align:center;">
                             <p:selectBooleanCheckbox value="#{file.visible}"/>
                </p:column>
                <h:panelGrid columns="2">
                        <p:commandButton value="Save" icon="fa fa-check" action="#{userFileController.updateFile()}" update="userFileTable" oncomplete="PF('editNoteDialog').hide()"/>
                        <p:commandButton value="Cancel" onclick="PF('editNoteDialog').hide()"/>
                    </h:panelGrid>
                
            </p:dialog>
              
        </h:form>
        
    </ui:define>
</ui:composition>
Da frage ich mich doch was da falsch ist.
Falls diese Codestücke zu ungenau sind siehe Anhang.

Eine Alternative die ich mir überlegt habe wäre einfach ein Logfile anzulegen für jeden User. Was kann ich da tun?
 

Anhänge

A

Anzeige


Vielleicht hilft dir dieser Kurs hier weiter: (hier klicken)
Passende Stellenanzeigen aus deiner Region:

Neue Themen

Oben