?
Solved

Unable to create a file using java.io.File

Posted on 2009-02-19
13
Medium Priority
?
271 Views
Last Modified: 2013-12-29
I  have attached my code

The problem is, no exception is thrown in
'File tempFile = new File("C://workspace//tempData//" + fileName);

but, a FileNotFoundException is thrown in
ZipFile zipFile = new ZipFile(tempFile);

Also, it specifies the Absolute Path, but when I check there, there is no file created.

Any help is appreciated.
TIA

public boolean validateZip(MultipartFile file, List<String> files) throws Exception {
		String fileName = file.getOriginalFilename();
		File tempFile = new File("C://workspace//tempData//" + fileName);
		LOG.info("Absolute path : " + tempFile.getAbsolutePath());
		ZipFile zipFile = new ZipFile(tempFile);
		LOG.info("No of enteries in the zip : " + zipFile.size());
		//loop through the list and check if the entry is in the zip file
 
	}

Open in new window

0
Comment
Question by:bent27
  • 6
  • 6
13 Comments
 
LVL 10

Assisted Solution

by:mahome
mahome earned 120 total points
ID: 23683094
You've only created a file instance, not file itself. Create a file:

File file = new File("test");
file.createNewFile();

Open in new window

0
 

Author Comment

by:bent27
ID: 23683451
mahome, I did try implementing

file.createNewFile();,  it doesn't seem to work...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23683769
Try the following:
File dir = new File("C:/workspace/tempData");
dir.mkdirs();
File tempFile = new File(dir, fileName);
LOG.info("Absolute path : " + tempFile.getAbsolutePath());
ZipFile zipFile = new ZipFile(tempFile);

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:bent27
ID: 23683958
CEHJ,  thanks a ton for your code, it works when I debug the application, but, if I try to run it, it throws the FileNotfoundException at this line ....




ZipFile zipFile = new ZipFile(tempFile);

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23684022
You have a logical error perhaps. Using that code (your last post) you are expecting a zip file to already exist with that path. Does one exist? I guess not...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23684293
If you need to create one - you need to use JarFile. afaik, ZipFile is read-only
0
 

Author Comment

by:bent27
ID: 23684339
my intention is to validate the contents in the ZipFile, so, is it that, when I debug, the app has time to actually write the tempFile to the correct location, but when I run the app, its quick, the file isn't created, and so, it throws the Exception...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23684512
Can you tell me what the value of 'tempFile' is?
0
 

Author Comment

by:bent27
ID: 23684956
'tempFile' is the file created from a multipartfile which the user has uploaded, now this multipartfile contains a zip file. I'm trying to validate the contents of the zipfile, in order to do that, I'm reading the multipartfile as a File, then, I'm reading it as a ZipFile...............

It works perfect when I debug the app, but when I run the app, the File from the multipartfile is not created and this causes the ZipFile creation to throw a FileNotFoundException....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23685006
OK, but what is the value of the String 'tempFile' ?
0
 

Author Comment

by:bent27
ID: 23685030
when I debug it, the tempFile instance is has only 1 field, thats 'path', and it indicates the location where its stored :

like : C:\\workspace\\tempData\\Sample.zip
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1380 total points
ID: 23685724
>>the File from the multipartfile is not created

So you create the file and then your code you posted above is there to validate it? How are you creating the file first with the multipart in it?

Incidentally you should create the file in your code using


C:/workspace/tempData/Sample.zip
0
 

Author Comment

by:bent27
ID: 23685760
Thanks CEHJ, apparently, this is a race condition, I've decided to implement apache commons FileUpload instead...,,
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question