473,718 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

madhoriya22
252 Contributor
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 18806
Nepomuk
3,112 Recognized Expert Specialist
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 MVP
..
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 Contributor
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 Recognized Expert Specialist
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" ).getAbsolutePa th() 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 Contributor
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" ).getAbsolutePa th() 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 Recognized Expert Specialist
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 Contributor
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 MultipartReques t.. 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
2638
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 simply run the appropriate command string through a standard "system()" call which is relatively simple. JAVA on the other hand seems to require something like RMI to do it.
4
1917
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 objOpenFile=objFSO.OpenTextFile(strPath,1) Do While Not objOpenFile.AtEndOfStream Response.Write(objOpenFile.ReadLine() & "<br>")
7
3371
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 communicate directly with the database. Do I need a separate db2 connect on each such machine. Please advice.
0
1312
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 enable propagation of inheritable permission on child folders. 4.- Read a file using Server.FileSystemObject into the web
7
4983
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 Parameter. I can call the first Method without Problems, the Parameter can be deserialized by the WebService. But if I want to call the second Method and give it an Array of Parameters, then the following exception is thrown by the WebService:...
2
1432
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 read file from client pc do i need to upload that file on server and then start reading that file. Or is there any other alternative for this.
2
1766
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 working , when i gives the path of the files to be read it looks on the local computer. I wants that applets should look the files in the web hosting server computer.
1
1795
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.*; import java.io.*;
1
21412
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 managed to do the client part. i read the content of a file, "hi.txt", and i send it to the server. The problem is in the server part, i do not know how to write the file in the server. Thanks in advance for your help Here is my code for the...
10
4248
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 active again and displays the messages. Here is my server code: import java.io.*; import java.net.*; public class serverForm extends javax.swing.JFrame { private PrintWriter output = null;
0
8827
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8724
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9053
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7991
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4481
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4743
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3182
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2123
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.