如何在目标中使用字符串

  • 本文关键字:字符串 目标 java jsf
  • 更新时间 :
  • 英文 :


希望是一个简单的问题:

我已经创建了代码来从登录页面获取用户名:

private String username;
@PostConstruct
public void init() {
    username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
}

但我想做的是将用户名添加到此目的地的末尾:

private String destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/";

我将如何做到这一点,以便目标将调用用户名以将文档放置在特定于该用户的文件中? 这一切都在同一个豆子里

这是我目前的豆子

@ManagedBean(name = "fileUploadController")
public class FileUploadController {
    private String username;
    @PostConstruct
    public void init() {
        username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
    }
    private String destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads
    File theFile = new File(destination + username); // will create a sub folder for each user 
    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        try {
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void copyFile(String fileName, InputStream in) {
        try {

            // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(new File(theFile + "/" + fileName)); // cannot find path when adding username atm
            System.out.println(theFile); //testing 
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            in.close();
            out.flush();
            out.close();
//make sure new file is created, (displays in glassfish server console not to end user)
            System.out.println("New file created!");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

当前编辑 :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package richard.fileupload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;
@ManagedBean(name = "fileUploadController")
public class FileUploadController {
    private String username;
    @PostConstruct
    public void init() {
        username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
    }
    private String destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads
    File theFile = new File(destination + username); // will create a sub folder for each user 
    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        try {
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public File getDirectory(String destination, String username) {
        //set the user directory from the destinarion and the logged user name
        File directory = new File(destination, username);
        //check if the location exists
        if (!directory.exists()) {
            //let's try to create it
            try {
                directory.mkdir();
            } catch (SecurityException secEx) {
                //handle the exception
                secEx.printStackTrace(System.out);
                directory = null;
            }
        }
        return directory;
    }

    public void copyFile(String fileName, InputStream in) {
        try {

            // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(new File(directory)); // cannot find path when adding username atm
            System.out.println(directory); //testing 
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            in.close();
            out.flush();
            out.close();
//make sure new file is created, (displays in glassfish server console not to end user)
            System.out.println("New file created!");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

编辑::::

现在说目录,在out = new FileOutputStream(new File(directory));找不到符号也我得到realFile没有使用

当前代码 :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package richard.fileupload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;
@ManagedBean(name = "fileUploadController")
public class FileUploadController {
    private String username;
    @PostConstruct
    public void init() {
        username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
    }
    private String destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads
    File theFile = new File(destination + username); // will create a sub folder for each user (currently does not work, below hopefully is a solution) 
    public File getDirectory(String destination, String username) { // currently not working, is not calling the username or directory 
        //set the user directory from the destinarion and the logged user name
        File directory = new File(destination, username);
        //check if the location exists
        if (!directory.exists()) {
            //let's try to create it
            try {
                directory.mkdir();
            } catch (SecurityException secEx) {
                //handle the exception
                secEx.printStackTrace(System.out);
                directory = null;
            }
        }
        return directory;
    }
    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        try {
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (IOException e) {
            //handle the exception
            e.printStackTrace();
        }
    }
 public void copyFile(String fileName, InputStream in) {
    //get the directory assigned to the current user
    File userDirectory = getDirectory(destination, username);
    if (userDirectory != null) {
        OutputStream out = null;
        try {
            File realFile = new File(userDirectory, fileName);
            out = new FileOutputStream(new File(directory));
            int read = 0;
            //1024 must be a constant
            //also, it must be 4098 (4 KBs) for better performance
            byte[] bytes = new byte[1024];
            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            in.close();
            out.flush();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            out.close();
        }
    }
}

再次编辑大声笑:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package richard.fileupload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;
@ManagedBean(name = "fileUploadController")
public class FileUploadController {
    private String username;
    @PostConstruct
    public void init() {
        username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
    }
    private String destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads (will change this thanks to adivce )
    File theFile = new File(destination + username); // will create a sub folder for each user (currently does not work, below hopefully is a solution) 
    public File getDirectory(String destination, String username) { 
        // currently not working, is not calling the username or destination 
        //set the user directory from the destinarion and the logged user name
        File directory = new File(destination, username);
        //check if the location exists
        if (!directory.exists()) {
            //let's try to create it
            try {
                directory.mkdir();
            } catch (SecurityException secEx) {
                //handle the exception
                secEx.printStackTrace(System.out);
                directory = null;
            }
        }
        return directory;
    }
    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        try {
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (IOException e) {
            //handle the exception
            e.printStackTrace();
        }
    }
    public void copyFile(String fileName, InputStream in) {
    //get the directory assigned to the current user
    File userDirectory = getDirectory(destination, username);
    if (userDirectory != null) {
        OutputStream out;
        try {
            File realFile = new File(userDirectory, fileName);//realFile variable not used
            out = new FileOutputStream(new File(userDirectory));// no suitable constructor found for File(File)
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            in.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
}
    /**
    public void copyFile(String fileName, InputStream in) {
        try {

            // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(new File(directory)); // cannot find path when adding username atm
            System.out.println(directory); //testing 
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            in.close();
            out.flush();
            out.close();
//make sure new file is created, (displays in glassfish server console not to end user)
            System.out.println("New file created!");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
*/

这是我的最新版本的代码,我尝试修改它,但它当前抛出了一些错误,我在代码的注释中添加了错误,公共文件是否getDirectory(String destination, String username)在正确的位置?谢谢,我从没想过我必须做这么多工作才能做一些简单的事情!:D

您的问题与File类使用更相关,而不是 JSF 问题。

可以使用此类来处理文件或目录。首先,您可以使用两种方法:

  • 将检索用户目录的方法

  • 将文件保存在用户目录中的方法

基本示例:

//note: this must be moved to an utility class
public File getDirectory(String destination, String username) {
    //set the user directory from the destinarion and the logged user name
    File directory = new File(destination, username);
    //check if the location exists
    if (!directory.exists()) {
        //let's try to create it
        try {
            directory.mkdir();
        } catch (SecurityException secEx) {
            //handle the exception
            //this is a naive way to do it
            secEx.printStackTrace(System.out);
            directory = null;
        }
    }
    return directory;
}
public void saveFile(String fileName, byte[] data) {
    //get the directory assigned to the current user
    File userDirectory = getDirectory(destination, username);
    if (userDirectory != null) {
        //create the real file using the directory as parent directory
        //we'll give the file name too
        //check the different constructors for File class
        File realFile = new File(userDirectory, fileName);
        //save the file the way you want/need...
        //this should be also in an utility class
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(realFile);
            fos.write(myByteArray);
        } catch (Exception ex) {
            //handle the exception
            ex.printStackTrace(System.out);
        } finally {
            if (fos != null)
                fos.close();
        }
    }
}

引用:

  • byte[] 到 Java 中的文件
  • 文件
  • 输出流(文件文件)构造函数

根据您的问题编辑,您必须修改copyFile才能看起来像我提出的saveFile方法。我将更改实现以使用InputStream.

public void copyFile(String fileName, InputStream in) {
    //get the directory assigned to the current user
    File userDirectory = getDirectory(destination, username);
    if (userDirectory != null) {
        OutputStream out;
        try {
            File realFile = new File(userDirectory, fileName);
            out = new FileOutputStream(new File(userDirectory));
            int read = 0;
            //1024 must be a constant
            //also, it must be 4098 (4 KBs) for better performance
            byte[] bytes = new byte[1024];
            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            in.close();
            out.flush();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            out.close();
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新