Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 518
  • Last Modified:

Get file size - Servlet Help

Hi,

I got form on submit it is sending form fields and files to write(upload ) on server. I'm able to get names of source files but having problem getting the file size.

DiskFileUpload upload = new DiskFileUpload();
List items = upload.parseRequest(req);
Iterator iter = items.iterator();
Map map = new HashMap();
while (iter.hasNext()) {
                                FileItem item = (FileItem) iter.next();
                                if (item.isFormField())
                                    {
                                        String name = item.getFieldName();
                                        String value = item.getString().trim();
                                        map.put(name,value);
                                    }
                                 else
                                        {  
                         <!-- Calling Stored Procedures -->                  
                        File cfile=new File(item.getName());                      
                        cs1.setString(1,cfile.getName());
                        cs1.setString(2,"<<NEED FILE SIZE HERE>>");   ????????????????
                        cs1.setString(3,(String) map.get("lang"));
                        cs1.executeQuery();
                                }                        
                 }

=========================
How can I get source filesize corresponding to filename and pass it to "cs1.setString(2, "????????");

Thanks for your time
0
princehyderabad
Asked:
princehyderabad
  • 10
  • 9
  • 6
3 Solutions
 
neonlinesProject managerCommented:
// Get the number of bytes in the file
cfile..length();
0
 
neonlinesProject managerCommented:
>> cfile..length(); should be

cfile.length();

0
 
neonlinesProject managerCommented:
Actually cfile.length() will return long

to convert into ur setString to as follows


cs1.setString(2, ""+cfile.length());
0
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.

 
princehyderabadAuthor Commented:
cs1.setString(2, cfile.length());
Error: cannot resolve method setString(int, long)
0
 
aozarovCommented:
neonlines, I am not sure cfile.length() will work in this case (at least not until the file was saved).
I think you should call:
cs1.setString(2, item.getSize());
0
 
aozarovCommented:
cs1.setString(2, String.valueOf(cfile.length())); // I don't think this should work.
or
cs1.setString(2, String.valueOf(item.getSize());

which ever works for you.

0
 
neonlinesProject managerCommented:
Yes aozarov, I missed it :(

0
 
neonlinesProject managerCommented:
But,

If FileItem object is created from valid file then cfile.length() will give the size of the file

If it is not having valid file content, then item.getSize() also will not return any values

So, I think the code
cs1.setString(2, ""+cfile.length())
will work


Both
item.getSize()  and cfile.length() may fail if there is no valid file. So, the programmer should take care of it :)
 
0
 
princehyderabadAuthor Commented:
You are correct aozarov!!

can you also tell me when I retrive the value it is giving me in bytes how do I convert in MB and show it to user.
rs.getString(8); give me in byets.

int i = rs.getString(8) /1000 ; Will this work correct ??
0
 
princehyderabadAuthor Commented:
Also is there anywas to get only 'Extension' of file. ??
0
 
aozarovCommented:
>> how do I convert in MB and show it to user.
cs1.setString(2, String.valueOf(item.getSize() / 1024));

>> Also is there anywas to get only 'Extension' of file. ??
int index = cfile.getName().lastIndexOf(".");
if (index >= 0)
String ext = cfile.getName().substring(index + 1);
0
 
princehyderabadAuthor Commented:
well do u think it will be good idea to store size in MB or it will be good to show user value in MB.
I guess we can do .../1024 after retriving the value from DB and at the time of dipslaying to user
0
 
neonlinesProject managerCommented:
If u no need to do any calculation in bytes then store as MB in Db too

Bcaz, it's reduce ur column size
0
 
princehyderabadAuthor Commented:
oops guys need little more help.

item.getSize() is putting in 'bytes'  Well let see if I decided to put in bytes. okay here.
But do u know the code how to retrive in KB or MB without giving decial values.

Idea is if the file is small I want user to be shows file size as " xxx KB" rather then " .xx MB" nor "xxx.xx KB"
And if the file is big I want to diplay in "xx MB" rather than "xxxxxxx KB" nor "xx.xx MB"
Hope u understand.
0
 
aozarovCommented:
Then save it in KB and only in the presentation layer do :

int sizeInKB = ...
String sizeValue = sizeInKB > 1024 ? ((sizeInKB / 1024) + " MB") : (sizeInKB + " KB");
0
 
princehyderabadAuthor Commented:
int sizeInKB = (String) rs.getString(8);
Error: Inconvertiable type: cannot cast 'java.lang.string' to int

0
 
aozarovCommented:
Try:

int sizeInKB =  rs.getInt(8);
or
int sizeInKB = Integer.parseInt(rs.getString(8));
0
 
princehyderabadAuthor Commented:
Thanks :o)
0
 
aozarovCommented:
:-)
0
 
princehyderabadAuthor Commented:
Back ..

int sizeInKB = ...
String sizeValue = sizeInKB > 1024 ? ((sizeInKB / 1024) + " MB") : (sizeInKB + " KB");

You said to save in KB and using the above code. But my application something needs to work with file in Bytes as well (eg. ReadMe.txt, Serial.txt etc.,)

Can you hlep in future step using above code. Size is saved in bytes. Now I want to show to user either in  xx Bytes  xx KB or xx MB ??

Please help !!
0
 
aozarovCommented:
int kb = 1024;
int mg = kb * kb;
String sizeValue = sizeInBytes >= mg ? ((sizeInBytes / 1024) + " MB") : sizeInBytes >= kb ? (sizeInBytes + " KB") : sizeInBytes + " bytes"
0
 
princehyderabadAuthor Commented:
int kb = 1024;
 int mg = kb * kb;
while (rs1.next())
 {
  int sizeInBytes = Integer.parseInt(rs1.getString(9));
  String sizeValue = sizeInBytes >= mg ? ((sizeInBytes / 1024) + " MB") : sizeInBytes >= kb ? (sizeInBytes + " KB") : sizeInBytes + "Bytes";
  out.print( sizeValue);
 }

Somthing wrong here. Seems like minor for eg:
Instead of displaying 118 MB it is showing  121179 MB
Instead of displaying 96 KB it is showing  98304 KB
0
 
aozarovCommented:
Oops, right.
Try this:
String sizeValue = sizeInBytes >= mg ? ((sizeInBytes / mg) + " MB") : sizeInBytes >= kb ? ((sizeInBytes / kb) + " KB") : sizeInBytes + "Bytes";
0
 
princehyderabadAuthor Commented:
Excellent !! Thanks my Friend ;o)
0
 
aozarovCommented:
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 10
  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now