`我正在编写一个java代码,使用带有Servlet 3.0的JSF 2.0在HTTP服务器上传文件。当我试图将文件上传到本地系统时,它工作正常,但如果我在http_path变量中指定HTTP服务器url目录,则会出现“java.io.IOException:文件名,目录名称或卷标语法不正确”错误。
虽然调试它在file.createNewFile()行失败但是如果我在http_path变量中指定本地目录它不会给出任何问题;
有人可以告诉我http_path变量中给出的HTTP url路径是否正确或者我是否缺少任何重要代码?
我的Bean文件:
@ManagedBean
@RequestScoped
public class Bean {
private UploadedFile uploadedFile;
public String submit() throws IOException {
String fileName = FilenameUtils.getName(uploadedFile.getName());
System.out.println("Uploaded File name is " + fileName);
byte[] bytes = uploadedFile.getBytes();
String http_path = "http://`172.21.11.108`/myhttp/";
URLConnection http = new URL(http_path).openConnection();
http.connect();
System.out.println("permisssion" + http.getPermission());
System.out.println("Url is " + http.getURL());
System.out.println("String" + http.getConnectTimeout());
FileOutputStream fop = null;
System.out.println("File name " + http_path + fileName);
File file = new File(http_path+fileName);
System.out.println("File path" + file.getPath());
if (!file.exists()) {
file.createNewFile();
System.out.println("entered file created");
}
fop = new FileOutputStream(file);
System.out.println("File name " + http_path + fileName);
fop.write(bytes);
fop.flush();
fop.close();
return "success";
}
Glassfish日志中的错误:
INFO:文件名是servlet_code_upload.txt INFO:文件contentTypeis text / plain INFO:permisssion(java.net.SocketPermission
172.21.11.108
:80 connect,resolve)INFO:Url是http://172.21.11.108
/ myhttp / INFO:String0 INFO:文件名 http://172.21.11.108
/myhttp/servlet_code_upload.txt信息:文件 pathhttp:`172.21.11.108` \ myhttp \ servlet_code_upload.txt 警告:{bean.submit}:java.io.IOException:文件名,目录名或卷标语法不正确javax.faces.FacesException:
{bean.submit}:java.io.IOException:
的文件名,目录名或卷标语法不正确com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) 在javax.faces.component.UICommand.broadcast(UICommand.java:315)at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) ... 36更多引起:java.io.IOException:文件名,目录 名称或卷标语法不正确 java.io.WinNTFileSystem.createFileExclusively(Native Method)at java.io.File.createNewFile(File.java:883)at com.example.Bean.submit(Bean.java:59)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)