473,327 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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

Similar topics

1
by: ian maclure | last post by:
I'm writing a client-server app. Client controls Server which in turn configures and controls a bunch of hardware. I want to be able to start the server from my client. Now in C/C++ one could...
4
by: Arpan | last post by:
Consider the following code snippet: <% Dim objFSO,objOpenFile,strPath strPath=Server.MapPath("ADList1.txt") Set objFSO=Server.CreateObject("SCRIPTING.FILESYSTEMOBJECT") Set...
7
by: CT | last post by:
Hi, This might seem like a basic question but I have some doubts, please humour me. I have a client-server application using java where each client on each machine needs to directly...
0
by: Pedro Bautista | last post by:
Status: Unsolved and puzzling Steps to reproduce the error: 1.- Delete IUSER from server 2.- Reboot server (OS rebuilds IUSR) 3.- Assign IUSR read and execute permission on web folder and...
7
by: Christian Wilhelm | last post by:
Hi! I'm trying to call a Java WebService out of a .net Client. There are two Methods, one Method requires one Parameter of type Parameter, the other Method requires one Parameter of type...
2
by: archana | last post by:
Hi all, I am new to asp.net (learning phase). I have to develop application in asp.net to read file from client pc and display statistics of that file to client. So my question is that to...
2
by: praveen2gupta | last post by:
Hi In my application i wants to read a file through a applet. I am using jsp.etc, . when application is loaded through web server on the another computer. The applets is downloaded and starts...
1
by: Cybelle | last post by:
please anyone can help me to solve this stuff, this is our final ., theres an error and i dont know how to solve it., import java.awt.*; import java.awt.event.*; import java.net.*;...
1
by: diegoblin | last post by:
Hi, i kind of new to java and i want to transfer a file between a server and a client. I know i have to use InputStream and OutputStream, but i don't know how to do it properly. So far i think i've...
10
by: Elaine121 | last post by:
Hi i've been batteling for hours and can't seem to find the problem. When my server runs and I press the connect button the gui freezes until the client gui is terminated.. only then the gui becomes...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.