File upload Wicket:FileUploadPage不刷新文件路径
再一次,我对维克特有意见。我试图用我的类“FileUploadPanel”上传数据,该类在另一个页面“Class A”上实现: A类File upload Wicket:FileUploadPage不刷新文件路径,file-upload,wicket,File Upload,Wicket,再一次,我对维克特有意见。我试图用我的类“FileUploadPanel”上传数据,该类在另一个页面“Class A”上实现: A类 ... /* uploadfields for Picture and Video */ ArrayList<String> picExt = new ArrayList<String>(); ArrayList<String> videoExt = new ArrayList<String>(); picExt.a
...
/* uploadfields for Picture and Video */
ArrayList<String> picExt = new ArrayList<String>();
ArrayList<String> videoExt = new ArrayList<String>();
picExt.add("jpg");
videoExt.add("mp4");
final FileUploadPanel picUpload = new FileUploadPanel("picUpload", "C:\\", picExt);
final FileUploadPanel videoUpload = new FileUploadPanel("videoUpload", "C:\\", videoExt);
final Form form = new Form("form"){
protected void onSubmit() {
...
// Save the path of Video and Picture into Database
table.setVideo(videoUpload.getFilepath());
table.setPicture(picUpload.getFilepath());
...
}
...
。。。
/*图片和视频的上传字段*/
ArrayList picExt=新的ArrayList();
ArrayList videoExt=新的ArrayList();
picExt.add(“jpg”);
视频文本添加(“mp4”);
final FileUploadPanel picUpload=新的FileUploadPanel(“picUpload”,“C:\\”,picExt);
final FileUploadPanel videoUpload=新的FileUploadPanel(“videoUpload”,“C:\\”,videoExt);
最终表格=新表格(“表格”){
受保护的void onSubmit(){
...
//将视频和图片的路径保存到数据库中
table.setVideo(videoUpload.getFilepath());
table.setPicture(picUpload.getFilepath());
...
}
...
类文件上载面板
public class FileUploadPanel extends Panel {
private static final long serialVersionUID = -2059476447949908649L;
private FileUploadField fileUpload;
private String UPLOAD_FOLDER = "C:\\";
private String filepath = "";
private List<String> fileExtensions;
/**
* Constructor of this Class
* @param id the wicket-id
* @param uploadFolder the folder, in which the File will be uploaded
* @param fileExtensions List of Strings
*/
public FileUploadPanel(String id, String uploadFolder, List<String> fileExtensions) {
super(id);
this.UPLOAD_FOLDER = uploadFolder;
this.fileExtensions = fileExtensions;
add(fileUpload = new FileUploadField("fileUpload"));
}
@Override
public void onComponentTag(ComponentTag tag){
// If no file is selected on startup
if(fileUpload.getFileUpload() == null){
return;
}
final FileUpload uploadedFile = fileUpload.getFileUpload();
if (uploadedFile != null) {
// write to a new file,
File newFile = new File(UPLOAD_FOLDER
+ uploadedFile.getClientFileName());
filepath = UPLOAD_FOLDER + uploadedFile.getClientFileName();
// if file in upload-folder already exists -> delete it
if (newFile.exists()) {
newFile.delete();
}
try {
newFile.createNewFile();
uploadedFile.writeTo(newFile);
info("saved file: " + uploadedFile.getClientFileName());
} catch (Exception e) {
throw new IllegalStateException("Error");
}
}
}
public String getFilepath() {
return filepath;
}
公共类文件上载面板扩展面板{
私有静态最终长serialVersionUID=-20594764447949908649L;
私有文件上传字段文件上传;
私有字符串上传\u FOLDER=“C:\\”;
私有字符串filepath=“”;
私有列表文件扩展;
/**
*这个类的构造函数
*@param-id-wicket-id
*@param uploadFolder将上载文件的文件夹
*@param fileExtensions字符串列表
*/
公共文件上载面板(字符串id、字符串上载文件夹、列表文件扩展名){
超级(id);
this.UPLOAD_FOLDER=上传文件夹;
this.fileExtensions=fileExtensions;
添加(fileUpload=newfileuploadfield(“fileUpload”);
}
@凌驾
公共无效onComponentTag(ComponentTag标记){
//如果启动时未选择任何文件
if(fileUpload.getFileUpload()==null){
回来
}
final FileUpload uploadedFile=FileUpload.getFileUpload();
if(uploadedFile!=null){
//写入新文件,
File newFile=新文件(上载文件夹
+uploadedFile.getClientFileName());
filepath=UPLOAD_FOLDER+uploadedFile.getClientFileName();
//如果上传文件夹中的文件已经存在->删除它
if(newFile.exists()){
newFile.delete();
}
试一试{
createNewFile();
uploadedFile.writeTo(新文件);
信息(“保存的文件:+uploadedFile.getClientFileName());
}捕获(例外e){
抛出新的非法状态异常(“错误”);
}
}
}
公共字符串getFilepath(){
返回文件路径;
}
}
好吧,如果我在我的“A级”上使用提交按钮,图片和视频会保存在C:\,这到目前为止相当不错。我以为我终于和wicket相处融洽了,但我高兴得太早了
问题:正确的路径未保存在数据库中,数据库以“Class A”的形式处理
我真的不明白,因为我的FileUploadPanel的onComponentTag(…)必须在使用submit按钮时执行。这是因为我在onComponentTag(…)中添加了一些验证,如“图片必须是JPG或不会被保存”,这很有效。所以我确信onComponentTag(…)在使用表单的提交按钮时执行,这也意味着文件路径应该是最新的
这次我做错了什么
提前感谢
问候语
V1nc3nt您正在使用
File newFile=新文件(上载文件夹+
uploadedFile.getClientFileName())
此代码用于创建新文件
相反,您可以尝试以下方法:
文件文件=新文件(上载文件夹,
uploadedFile.getClientFileName())
然后获得绝对路径和保存它:
newFile.getAbsolutePath()
谢谢你的回答。我试过了,但没有帮助。我刚刚发现,如果我上传第二个文件,它有第一个文件的路径。可能是,类FileUploadPanel的onComponentTag(…)在类a中的表单的submit按钮被按下后被执行吗?我当时所做的,是覆盖onComponentTag(…)类FileUploadPanel的构造函数中FileUploadField的。一旦选择了文件,就会更新此字段,因此我认为调用:FileUploadPanel.This.onComponentTag(tag)可能会有所帮助;在它里面。但是它也不起作用…你是在使用wicket 1.4还是WikSet>=1.5?FileUploadField从1.4.x到1.5.x的不同之处在于现在它支持(HTML5标准),并且它使用列表而不仅仅是FileUpload。请看这个公共的java.util.List getFileUploads()返回:所有上传文件的列表。我找到了一个“解决方案”我只是在获取源路径之前以类A的形式调用它:
picUpload.onComponentTag(null);videoUpload.onComponentTag(null);
我希望有更好的方法=/