469,342 Members | 5,481 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

java.io.FileNotFoundException: while reading the file from client to server

madhoriya22
252 100+
Hi,
I am trying to read a file sent from client to server. Here is how I am trying it ....
Expand|Select|Wrap|Line Numbers
  1. String filePath = request.getParameter("SelectCSVFile"); 
  2. System.out.println("path:::---> "+filePath);
  3. String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
  4. System.out.println("File Name:\t"+fileName);
  5.  
  6. try {
  7. File file = new File(filePath);
  8. URL serverURL = new URL(SERVER_URL);
  9. URLConnection serverCon = serverURL.openConnection();
  10. // setup connection
  11. serverCon.setDoInput(true);
  12. serverCon.setDoOutput(true);
  13. serverCon.setUseCaches(false);
  14. serverCon.setRequestProperty(FILENAME_HEADER, fileName); // send the filename through HTTP header
  15. System.out.println("Successfully set the header");
  16. // POST the file's bytes to the connections OutputStream
  17. OutputStream toServer = serverCon.getOutputStream();
  18. System.out.println("Successfully get the servlet output Stream");
  19. FileInputStream fromFile = new FileInputStream(file);//at this line it is throwing the exception
  20.  
It is throwing the exception ..... here is the server log
Expand|Select|Wrap|Line Numbers
  1.  
  2. path:::---> C:/Documents and Settings/harbeer.kadian/My Documents/bugs-2007-09-07.csv//here separators are '/'
  3. File Name: bugs-2007-09-07.csv
  4. Successfully get the servlet output Stream
  5. java.io.FileNotFoundException: C:\Documents and Settings\harbeer.kadian\My Documents\bugs-2007-09-07.csv //here separators are '\'(The system cannot find the path specified)
  6.         at java.io.FileInputStream.open(Native Method)
  7.         at java.io.FileInputStream.<init>(FileInputStream.java:106)
  8.         at com.spi.defecttracker.controller.RequestHandler.uploadCSVFileData(RequestHandler.java:1486)
  9.         at com.spi.defecttracker.controller.RequestHandler.processRequest(RequestHandler.java:164)
  10.         at com.spi.defecttracker.controller.RequestHandler.doPost(RequestHandler.java:348)
  11.         at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
  12.         at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  13.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
  14.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
  15.         at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
  16.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
  17.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
  18.         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
  19.         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
  20.         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
  21.         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
  22.         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
  23.         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
  24.         at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
  25.         at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
  26.         at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
  27.         at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
  28.         at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
  29.         at java.lang.Thread.run(Thread.java:595)
  30.  
The file path I am sending is using '/'(forward slash) as separator while the path exception is showing is using '\'(backward slash). How is it happening. How can I resolve it.
Sep 18 '07 #1
7 18376
Nepomuk
3,112 Expert 2GB
Hi,
I am trying to read a file sent from client to server. Here is how I am trying it ....
Expand|Select|Wrap|Line Numbers
  1. String filePath = request.getParameter("SelectCSVFile"); 
  2. System.out.println("path:::---> "+filePath);
  3. String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
  4. System.out.println("File Name:\t"+fileName);
  5.  
  6. try {
  7. File file = new File(filePath);
  8. URL serverURL = new URL(SERVER_URL);
  9. URLConnection serverCon = serverURL.openConnection();
  10. // setup connection
  11. serverCon.setDoInput(true);
  12. serverCon.setDoOutput(true);
  13. serverCon.setUseCaches(false);
  14. serverCon.setRequestProperty(FILENAME_HEADER, fileName); // send the filename through HTTP header
  15. System.out.println("Successfully set the header");
  16. // POST the file's bytes to the connections OutputStream
  17. OutputStream toServer = serverCon.getOutputStream();
  18. System.out.println("Successfully get the servlet output Stream");
  19. FileInputStream fromFile = new FileInputStream(file);//at this line it is throwing the exception
  20.  
It is throwing the exception ..... here is the server log
Expand|Select|Wrap|Line Numbers
  1.  
  2. path:::---> C:/Documents and Settings/harbeer.kadian/My Documents/bugs-2007-09-07.csv//here separators are '/'
  3. File Name: bugs-2007-09-07.csv
  4. Successfully get the servlet output Stream
  5. java.io.FileNotFoundException: C:\Documents and Settings\harbeer.kadian\My Documents\bugs-2007-09-07.csv //here separators are '\'(The system cannot find the path specified)
  6.         at java.io.FileInputStream.open(Native Method)
  7.         at java.io.FileInputStream.<init>(FileInputStream.java:106)
  8.         at com.spi.defecttracker.controller.RequestHandler.uploadCSVFileData(RequestHandler.java:1486)
  9.         at com.spi.defecttracker.controller.RequestHandler.processRequest(RequestHandler.java:164)
  10.         at com.spi.defecttracker.controller.RequestHandler.doPost(RequestHandler.java:348)
  11.         at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
  12.         at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  13.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
  14.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
  15.         at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
  16.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
  17.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
  18.         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
  19.         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
  20.         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
  21.         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
  22.         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
  23.         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
  24.         at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
  25.         at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
  26.         at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
  27.         at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
  28.         at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
  29.         at java.lang.Thread.run(Thread.java:595)
  30.  
The file path I am sending is using '/'(forward slash) as separator while the path exception is showing is using '\'(backward slash). How is it happening. How can I resolve it.
Hi madhoriya22!
Is there any specific reason known to you, why
Expand|Select|Wrap|Line Numbers
  1. System.out.println("Successfully set the header"); // line 15
  2.  
doesn't create an output? Also, what does
Expand|Select|Wrap|Line Numbers
  1. System.out.println("File exists: " + file.exists());
  2.  
say? And why are you using '/' as a file separator, when the standard file separator under Windows is '\\'?

Greetings,
Nepomuk

*QUESTION TO THE ADMINS & CO: What is this code-tag-bug? It didn't let me create a java-code-section with some code but it did allow a text-code-section with the same code. Weird...*
Sep 18 '07 #2
r035198x
13,262 8TB
..
Greetings,
Nepomuk

*QUESTION TO THE ADMINS & CO: What is this code-tag-bug? It didn't let me create a java-code-section with some code but it did allow a text-code-section with the same code. Weird...*
The code tags bug (as I know it) is that when you quote a post with long lines of code posted under code tags then chances are high that that post that you make will appear as a blank post.

Edit: As evidenced by snowfall's second post here. The first post was also hit by the bug but I removed the quote from it through the edit facility.
Sep 18 '07 #3
madhoriya22
252 100+
Hi madhoriya22!
Is there any specific reason known to you, why
Expand|Select|Wrap|Line Numbers
  1. System.out.println("Successfully set the header"); // line 15
  2.  
doesn't create an output? Also, what does
Expand|Select|Wrap|Line Numbers
  1. System.out.println("File exists: " + file.exists());
  2.  
say? And why are you using '/' as a file separator, when the standard file separator under Windows is '\\'?

Greetings,
Nepomuk

*QUESTION TO THE ADMINS & CO: What is this code-tag-bug? It didn't let me create a java-code-section with some code but it did allow a text-code-section with the same code. Weird...*
Hi nepomuk,
By mistake I copied that line. //line 15

Expand|Select|Wrap|Line Numbers
  1. System.out.println("File exists: " + file.exists());
  2.  
and its not working .. means not able to find the file .....
Thats what I want to ask ......
How can i make it to find the file in client's system(The file I am supplying is on client's machine).

'/' separator I was using because while while getting the file name from the path supplied ... it was giving compile time error here
Expand|Select|Wrap|Line Numbers
  1. String fileName = filePath.substring(filePath.lastIndexOf("/")+1);//here it was not taking back slash(\)
  2.  
But now I have resolved it .... by doing like this ..
Expand|Select|Wrap|Line Numbers
  1. String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
  2.  
So pls give ur suggestions on that ..
Sep 19 '07 #4
Nepomuk
3,112 Expert 2GB
Hi nepomuk,
By mistake I copied that line. //line 15

Expand|Select|Wrap|Line Numbers
  1. System.out.println("File exists: " + file.exists());
  2.  
and its not working .. means not able to find the file .....
OK, that reduces the possibilities of what the mistake might be, as it's something with the Path.
How can i make it to find the file in client's system(The file I am supplying is on client's machine).
This program is running on the client's computer, isn't it? If it's running on server side, you'll not be able to do it like that. (And I don't know, how you could do it at all with Java, but maybe someone else does?)
'/' separator I was using because while while getting the file name from the path supplied ... it was giving compile time error here
Expand|Select|Wrap|Line Numbers
  1. String fileName = filePath.substring(filePath.lastIndexOf("/")+1);//here it was not taking back slash(\)
But now I have resolved it .... by doing like this ..
Expand|Select|Wrap|Line Numbers
  1. String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
  2.  
The path on a windows system is normally separated with '\\', but as far as I know, '/' is possible. However, new File("this.txt").getAbsolutePath() for example will supply a Path using '\\'.

If you don't know, what separator will be used, you can't do it quite like that - you'll have to check:
Expand|Select|Wrap|Line Numbers
  1. String fileName;
  2. if(filePath.indexOf('/') != -1) fileName = filePath.substring(filePath.lastIndexOf("/")+1);
  3. else if(filePath.indexOf('\\') != -1) fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
  4. else fileName = filePath; // just to make sure
  5.  
or (and this might solve your problem for you):
Expand|Select|Wrap|Line Numbers
  1. filePath = filePath().replace('\\', '/');
  2. String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
  3.  
With this, you make sure, that there will only be '\\'s and no '/'s in your path.

Does that help?

Greetings,
Nepomuk
Sep 19 '07 #5
madhoriya22
252 100+
OK, that reduces the possibilities of what the mistake might be, as it's something with the Path.
This program is running on the client's computer, isn't it? If it's running on server side, you'll not be able to do it like that. (And I don't know, how you could do it at all with Java, but maybe someone else does?)
The path on a windows system is normally separated with '\\', but as far as I know, '/' is possible. However, new File("this.txt").getAbsolutePath() for example will supply a Path using '\\'.

If you don't know, what separator will be used, you can't do it quite like that - you'll have to check:
Expand|Select|Wrap|Line Numbers
  1. String fileName;
  2. if(filePath.indexOf('/') != -1) fileName = filePath.substring(filePath.lastIndexOf("/")+1);
  3. else if(filePath.indexOf('\\') != -1) fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
  4. else fileName = filePath; // just to make sure
  5.  
or (and this might solve your problem for you):
Expand|Select|Wrap|Line Numbers
  1. filePath = filePath().replace('\\', '/');
  2. String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
  3.  
With this, you make sure, that there will only be '\\'s and no '/'s in your path.

Does that help?

Greetings,
Nepomuk
Hi,
Thanks buddy .... I got your point :)

Does anyone know how to get the file from client to server?
Sep 19 '07 #6
Nepomuk
3,112 Expert 2GB
Hi,
Thanks buddy .... I got your point :)

Does anyone know how to get the file from client to server?
If you're prepared to use another language for that task, this thread might help you.

Greetings,
Nepomuk
Sep 19 '07 #7
madhoriya22
252 100+
If you're prepared to use another language for that task, this thread might help you.

Greetings,
Nepomuk
Hi,
I can not use other language. I have to do this in java. I wish I could make use of MultipartRequest.. But my html form is not using the encoding type which is required for multipart/form-data.
Anyway thanks for your suggestion :)
Sep 19 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by ian maclure | last post: by
4 posts views Thread by Arpan | last post: by
7 posts views Thread by CT | last post: by
7 posts views Thread by Christian Wilhelm | last post: by
2 posts views Thread by archana | last post: by
1 post views Thread by Cybelle | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.