2013-04-30 77 views
0

我卡住了......我跟着几个教程,但我不知道我的问题是什么......我需要在Repertoir C:\中加载文件 但我不明白做我卡在文件上传(allways不正在上传)

这是我

<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> 

这是我对豆

@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()); 
} 
} 
} 

梅索德这是它allaws显示我的错误消息时上传文件

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) 
+0

当我加入这个 ​​uploadDirectory d:\ PDF FERESSS 2013-04-30 09:28:30

+0

它工作正常..why? – FERESSS 2013-04-30 09:28:46

回答

0

的问题是,event.getFile()的getFileName()是完整路径返回文件名

所以我改变我的梅索德像这样

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

下议院IO提供FilenameUtils#的getName ()的确切目的。 想你们都:)