0

`I am writing a java code to upload files in HTTP server using JSF 2.0 wtih Servlet 3.0. When i am trying to uplaod the file to local system it works fine but if I specify the HTTP server url directory in http_path variable it gives "java.io.IOException: The filename, directory name, or volume label syntax is incorrect" error.

  • Also there are no issue while accessing the HTTP location using Web browser and i was successfully able to download files via java code if i give the HTTP path given in http_path(used in Bean.java)
  • While debugging it fails at file.createNewFile() line but if i specify local directory in http_path variable it doesnt give any problem;

    Can someone please let me know whether the HTTP url path given in http_path variable is correct or am i missing any important code?

My Bean file:

@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";

    }

Error in Glassfish logs:

INFO: File name is servlet_code_upload.txt INFO: File contentTypeis text/plain INFO: permisssion(java.net.SocketPermission 172.21.11.108:80 connect,resolve) INFO: Url is http://172.21.11.108/myhttp/ INFO: String0 INFO: File name http://172.21.11.108/myhttp/servlet_code_upload.txt INFO: File pathhttp:`172.21.11.108`\myhttp\servlet_code_upload.txt WARNING:

{bean.submit}: java.io.IOException: The filename, directory name, or volume label syntax is incorrect javax.faces.FacesException:

{bean.submit}: java.io.IOException: The filename, directory name, or volume label syntax is incorrect at

com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) at javax.faces.component.UICommand.broadcast(UICommand.java:315) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) ... 36 more Caused by: java.io.IOException: The filename, directory name, or volume label syntax is incorrect at 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)

SJUmbarkar
  • 3
  • 1
  • 5

0 Answers0