希望是一个简单的问题:
我创建了从登录页面获取用户名的代码:
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