うまくいけば、簡単な質問:
ログインページからユーザー名を取得するコードを作成しました:
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/";
宛先がユーザー名を呼び出して、そのユーザーに固有のファイルにドキュメントを配置するようにするにはどうすればよいですか? これはすべて同じ Bean にあります
これは私の現在の豆です
@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