0

我被卡住了......我遵循了几个教程,但我不知道我的问题是什么......我需要在 Repertoir C:\ PDF 中上传文件,但我没有做

这是我的

<h:form enctype="multipart/form-data">

<p:fileUpload

mode="advanced"

fileUploadListener="#{composantbean.upload}"

allowTypes="/(\.|\/)(gif|jpe?g|png)$/"  sizeLimit="100000" description="Select Images"

auto="true"/>

</h:form>

这是我在 bean 上的方法

@ManagedBean(name="composantbean")
@SessionScoped
public class Composantbeam {
private String destination="C:\\PDF\\";

public void upload(FileUploadEvent event) { 
FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded."); 
FacesContext.getCurrentInstance().addMessage(null, msg);
// Do what you want with the file       
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(destination + fileName));
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();
System.out.println("New file created!");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

这是我上传文件时显示的错误消息

    C:\PDF\C:\Documents and Settings\Admin\Bureau\login.png (Syntaxe du nom de fichier, de répertoire ou de volume incorrecte)

C: \ PDF \ C: \ Documents and Settings \ Admin \ Desktop \ login.png (syntax file name, directory or incorrect volume)
4

1 回答 1

0

问题是 event.getFile().getFileName() 正在返回具有完整路径的文件名

所以我改变了我的方法像这样

File result = new File(destination+ ***FilenameUtils.getName***(event.getFile().getFileName()));

Commons IO 提供 FilenameUtils#getName() 用于确切目的。想你们了:)

于 2013-04-30T13:17:36.793 回答