如何从 Vaadin 中的上传组件获取文件
How to get the File from an Upload component in Vaadin
我是 Vaadin 的新手,我不知道如何从上传组件中获取文件,我总是遇到服务器错误。
我不认为我正确理解这是如何工作的。欢迎任何帮助。
这是我的代码。
public MainView() {
HorizontalLayout horizontalLayout = new HorizontalLayout();
horizontalLayout.add(
new H1("Stop Motion Creator")
);
add(horizontalLayout);
MemoryBuffer memoryBuffer = new MemoryBuffer();
Upload midiFileUpload = new Upload(memoryBuffer);
midiFileUpload.setMaxFiles(1);
midiFileUpload.setDropLabel(new Label("Upload a file in .mid format"));
midiFileUpload.setAcceptedFileTypes("mid");
midiFileUpload.setMaxFileSize(10);
midiFileUpload.addSucceededListener(event -> {
InputStream inputFileData = memoryBuffer.getInputStream();
String fileName = event.getFileName();
long contentLength = event.getContentLength();
String mimeType = event.getMIMEType();
Receiver receiver = new Receiver() {
@Override
public OutputStream receiveUpload(String s, String s1) {
return null;
}
};
OutputStream outputFileData = receiver.receiveUpload(fileName, mimeType);
FileData filedata = new FileData(fileName, mimeType,outputFileData);
File midiFile = filedata.getFile();
}
这是我的一个项目中的代码示例
FileUploader buffer = new FileUploader();
upload.setReceiver(buffer);
upload.setAcceptedFileTypes("image/jpeg", "image/png");
upload.addSucceededListener(event -> {
try {
File file = new File(buffer.getFilename());
// Do whatever with file object
} catch (IOException e) {
e.printStackTrace();
}
});
FlieUploader.java
public class FileUploader implements Receiver {
public static String BASE_PATH = "/tmp/";
private File file;
private String filename;
public OutputStream receiveUpload(String filename,
String mimeType) {
// Create upload stream
FileOutputStream fos = null; // Stream to write to
try {
// Open the file for writing.
this.filename = FileUploader.BASE_PATH + filename;
file = new File(this.filename);
fos = new FileOutputStream(file);
} catch (final java.io.FileNotFoundException e) {
return null;
}
return fos; // Return the output stream to write to
}
public String getFilename() {
return filename;
}
};
我是 Vaadin 的新手,我不知道如何从上传组件中获取文件,我总是遇到服务器错误。 我不认为我正确理解这是如何工作的。欢迎任何帮助。 这是我的代码。
public MainView() {
HorizontalLayout horizontalLayout = new HorizontalLayout();
horizontalLayout.add(
new H1("Stop Motion Creator")
);
add(horizontalLayout);
MemoryBuffer memoryBuffer = new MemoryBuffer();
Upload midiFileUpload = new Upload(memoryBuffer);
midiFileUpload.setMaxFiles(1);
midiFileUpload.setDropLabel(new Label("Upload a file in .mid format"));
midiFileUpload.setAcceptedFileTypes("mid");
midiFileUpload.setMaxFileSize(10);
midiFileUpload.addSucceededListener(event -> {
InputStream inputFileData = memoryBuffer.getInputStream();
String fileName = event.getFileName();
long contentLength = event.getContentLength();
String mimeType = event.getMIMEType();
Receiver receiver = new Receiver() {
@Override
public OutputStream receiveUpload(String s, String s1) {
return null;
}
};
OutputStream outputFileData = receiver.receiveUpload(fileName, mimeType);
FileData filedata = new FileData(fileName, mimeType,outputFileData);
File midiFile = filedata.getFile();
}
这是我的一个项目中的代码示例
FileUploader buffer = new FileUploader();
upload.setReceiver(buffer);
upload.setAcceptedFileTypes("image/jpeg", "image/png");
upload.addSucceededListener(event -> {
try {
File file = new File(buffer.getFilename());
// Do whatever with file object
} catch (IOException e) {
e.printStackTrace();
}
});
FlieUploader.java
public class FileUploader implements Receiver {
public static String BASE_PATH = "/tmp/";
private File file;
private String filename;
public OutputStream receiveUpload(String filename,
String mimeType) {
// Create upload stream
FileOutputStream fos = null; // Stream to write to
try {
// Open the file for writing.
this.filename = FileUploader.BASE_PATH + filename;
file = new File(this.filename);
fos = new FileOutputStream(file);
} catch (final java.io.FileNotFoundException e) {
return null;
}
return fos; // Return the output stream to write to
}
public String getFilename() {
return filename;
}
};