This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

copy file - size file 0

 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i read a file and copy to another file....

new file size is alway 0...



any idea?
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think request.getParameter("file") return String of filename. You check filename and sure it exist.
 
Bartender
Posts: 9588
13
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you see characters being printed out on the console?
It's always a good idea to flush() an output stream before closing it.
I see you are getting the input file name from a request instance. Are you performing this code in a servlet? If so I hope both your source and destination are on the server. If you are passing a file name thinking that it will upload data to the servlet, you have a little more work to do.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For any Writer, the close() method is supposed to implicltly call flush() first.If you ever discover that's not occurring, please file a bug report. For OutputStream the situation is a bit more vague, as the API doesn't directly say that close() implies flush(). Though I would assert that any competnetly-designed stream should flush as part of a close - but it's not guaranteed by the API.

The code you show looks OK - I think the most likely problem is that the input file has size 0. Joe's question "do you see characters being printed out on the console?" is key. You may also want to insert additional tests:
 
mark smith
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
For any Writer, the close() method is supposed to implicltly call flush() first.If you ever discover that's not occurring, please file a bug report. For OutputStream the situation is a bit more vague, as the API doesn't directly say that close() implies flush(). Though I would assert that any competnetly-designed stream should flush as part of a close - but it's not guaranteed by the API.

The code you show looks OK - I think the most likely problem is that the input file has size 0. Joe's question "do you see characters being printed out on the console?" is key. You may also want to insert additional tests:



ok i get:

inFile path: /home/paul/GridBagLayoutDemo.java
inFile size: 133

ya i see printed characters


ya i use a servlet....

source and destination file are on the server..... but i would like to be able to do send local file to a remote server....
 
Joe Ess
Bartender
Posts: 9588
13
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File uploading is usually accomplished using a multipart form. I posted my favorite links on the subject in this thread.
As for the problem you are having, you may want to print out the path to your destination file as well, to make sure you are looking at the correct file.
 
mark smith
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i use uploadfile library from apache....

i try to send only a file with fileupload and save the file to the server

my jsp file
<form action="Upload" method="POST" enctypetype="multipart/form-data">
<input type="file" name="file">
<input type="SUBMIT" name="button" value="Envoie">
</form>

my servlet

DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);

try{
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
if (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
System.out.println(item.getName());
File uploadedFile = new File(item.getName());
try{
item.write(uploadedFile);
}catch (Exception e) {
e.printStackTrace();
}
}
}catch (FileUploadException e) {
e.printStackTrace();
}

is it the correct way to upload and file file?

why i try to upload i file i get:

javax.servlet.ServletException: error cannot instantiate servlet Upload class

org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
java.lang.Thread.run(Thread.java:534)


java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException
java.lang.Class.getDeclaredConstructors0(Native Method)
java.lang.Class.privateGetDeclaredConstructors(Class.java:1618)
java.lang.Class.getConstructor0(Class.java:1930)
java.lang.Class.newInstance0(Class.java:278)
java.lang.Class.newInstance(Class.java:261)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
java.lang.Thread.run(Thread.java:534)
[ March 10, 2006: Message edited by: mark smith ]
 
Sheriff
Posts: 24658
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException <<

is the key piece of information. You need that class in your classpath, which in the case of servlets means putting the jar file that contains the class into your web application's WEB-INF/lib directory.
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!