Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Java
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 03-27-2006, 04:47 AM   #1 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
Log4j created file not able to delete while testing my application??

Hi,

1)
In my web-based application i am using log4j for debugging my application.But the problem i m facing is that whenever i have to delete the logs generated while the Tomcat Server is on that is i am testing my application, i am able to delete the logs but not able to save the file so all the logs i deleted diplays again plus the new one generated.The system
says the file is in use?? So i have to shutdown my tomcat & then the file gets saved .So how to get rid of this problem .

Also how can i generate different files whenever i start my application so that i dont have to refer my old file for the logs.Does it need any extra configuration in my properties file i m using for log4j??

My properties file is like this.....

Code:
# # Log for Java configuration don't change unless you know what you doing # The possible values here are debug, info, warn, error, fatal # #log4j.rootCategory=info, R log4j.rootCategory=debug, R # # Dont change this # log4j.appender.R=org.apache.log4j.RollingFileAppender # # This is the file that becomes the log file. Older log files are renamed as fileName1 fileName2 etc. # #log4j.appender.R.File=C:\\jakarta-tomcat-4.0.1\\webapps\\FileXDMS\\WEB-INF\\fileXDMS.log log4j.appender.R.File=C:/JIGNESH/gmg.log # # The maximum size of the log file, good idea to keep the size small. # log4j.appender.R.MaxFileSize=100KB # # This defines the no of log files to keep. Since this is a rolling file system, after the number of # files reaches this number, the oldest file is rewritten # log4j.appender.R.MaxBackupIndex=10 # # Dont change this # log4j.appender.R.layout=org.apache.log4j.PatternLayout # # This defines the way comments are written to the log file, for e.g. %d tells the logger to write the date # for a full list see the log4j manual. %p is the priority (debug, info etc as defined above) # log4j.appender.R.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} -- %p -- %m%n
2)
One more thing i am generating some csv files from my application but they doesn't get deleted while my server remains ON. This should not happen as my application will be used by many users through web..

What i want to do is create the csv files actually they are a form of reports so each individual user should be able to generate report in the file system of their own PC.Currently my files are getting generated on the fileSystem where i m developing my application, ie on server's file system...

So help me sort out these two queries...

Thanks,
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 03-27-2006, 12:39 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
This is taking place on a Windows machine?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 03-27-2006, 08:15 PM   #3 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
yes it on the Windows machine....
__________________
j.gohel is offline   Reply With Quote
Old 03-28-2006, 01:31 AM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
If you have a file open for writing in Windows, Windows will put a lock on it. This is different from Unix, where you can delete just about anything. You'll have trouble deleteing files until whatever program is accessing them gives up the lock.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 03-28-2006, 05:04 AM   #5 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
And what about creating different log4j file whenever my application starts newly everytime???

Also i am generating some report files through my application & this application would be used by many users across web..

So each individual user who would be using the application should be able to get that generated reports or rather generate in the file system of their own PC.Currently my files are getting generated on the fileSystem where i m developing & testing my application, ie on server's file system...So how can i make it possible to generate the files on user's local PC & not on server??

If its very difficult then suggest some alternatives regarding the same....

Thanks,
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 03-28-2006, 01:04 PM   #6 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
I'm honestly not familiar enough with log4j to know how to customize logfile creation, sorry.

As for sending the user a file, the best way to do it is using ServletResponse (it's an implicit variable available to JSPs, the "response" variable).

What you do is you send the client a MIME type, letting them know what kind of file it is, and then stream the data to them. This is secure because you're controlling access to the stream (based on a users session, I assume). You also don't need to take up diskspace, as you can generate the reports on the fly. However, the tradeoff is in CPU and memory utilization, so you might want to store them locally and then stream the files (as opposed to offer them up for download).

Quote:
Originally Posted by j.gohel
And what about creating different log4j file whenever my application starts newly everytime???

Also i am generating some report files through my application & this application would be used by many users across web..

So each individual user who would be using the application should be able to get that generated reports or rather generate in the file system of their own PC.Currently my files are getting generated on the fileSystem where i m developing & testing my application, ie on server's file system...So how can i make it possible to generate the files on user's local PC & not on server??

If its very difficult then suggest some alternatives regarding the same....

Thanks,
Jignesh
__________________
GitS
Belisarius is offline   Reply With Quote
Old 03-28-2006, 07:27 PM   #7 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
Thanks Belisarius for all your support.

If u dont mind can you please tell me the general steps like a sort of algorithm or some pseudocode to achieve this thing : generating the reports on the fly & sending it in response.My report type is .xls.& the design pattern i am using is MVC so all my work goes through a single controller.

Regards,
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 03-29-2006, 01:41 AM   #8 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
I've never done this, so I can't vouch for the efficiency or correctness of this. Also, I'm not really used to working with Streams, so that's another thing you'll have to test and work out.

Code:
FileInputStream fis = new FileInputStream(<file location>); int fileLength = fis.available(); byte[] bytes = new byte[fileLength]; fis.read(bytes); response.setContentType(<XLS content flag>); response.getOutputStream().write(bytes); response.getOutputStream().flush(); response.getOutputStream().close(); fis.close();
Alternately, you could do it this way:
Code:
FileInputStream fis = new FileInputStream(<file location>); int fileLength = fis.available(); response.setContentType(<XLS content flag>); for(int i =0; i < fileLength; i++){ byte current = fis.read(); response.getOutputStream().write(current); } response.getOutputStream().flush(); response.getOutputStream().close(); fis.close();
That may or may not be more efficient/use a smaller footprint. You'll have to experiment to find out.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 03-29-2006, 02:35 AM   #9 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
Thank u very much Belisarius for all ur cooperation.
Thanks once again.

Regards,
jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-04-2006, 10:08 PM   #10 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
Hi Belisarius,

response.getOutputStream().write(current);

Like this i will write the raw bytes to the response.
But on the user's file system i want to save this written bytes to a particular location so for that i will ahve to create some files in the user file system dynamically .So how to do that???

Thanks,
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-05-2006, 01:29 AM   #11 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
I'm not sure I follow what you want to do, but the end user should simply see this as an incoming file and save it as they would any other file.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-07-2006, 12:17 AM   #12 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
Log4j created file not able to delete while testing my application??

Hi,

Now, I am able to download & save the files as excel files.
But one problem:
When i have a single file i am able to download & save it easily.

But in some cases i have mutiple files to be downloaded & saved so how can i do this thing of downloading & saving multiple files coming in the response.

Also i am not able to delete my file on the server after downloading it

Here is my code for downloading my reports:

Code:
public boolean downloadReport(String dirPath,HttpServletResponse response) { FileInputStream fis = null; boolean flag = false; File f = new File(dirPath); ArrayList tempFileArrList = new ArrayList(); if(f.isDirectory()) { File[] fArray = f.listFiles(); for(int i = 0; i < fArray.length; i++) { log.debug("File Names Array Content : fArray [" + i + "]" + fArray[i]); if(fArray[i].exists()) { try { fis = new FileInputStream(fArray[i]); int fileLength = fis.available(); byte[] bytes = new byte[fileLength]; fis.read(bytes); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition","attachment; filename = \"myxlfile.xls\" "); response.getOutputStream().write(bytes); response.getOutputStream().flush(); //response.getOutputStream().close(); //fis.close(); tempFileArrList.add(fArray[i]); //fArray[i].delete(); log.debug("DEBUG 10"); flag = true; } catch(IOException ioe) { log.debug("IOException in GMGController while download "+ioe); flag = false; } catch(Exception e) { log.debug("Exception in GMGController while download "+e); flag = false; } } else { continue; } } try { response.getOutputStream().close(); fis.close(); flag = true; } catch(IOException ioe) { log.debug("IOException in GMGController while closing "+ioe); flag = false; } catch(Exception e) { log.debug("Exception in GMGController while closing "+e); flag = false; } } for(int i = 0; i < tempFileArrList.size(); i++ ) { File file = (File)tempFileArrList.get(i); if(file.exists()) { log.info("Deleting file "+ (i+1) + " "+ file.getName()); file.delete(); } } return flag; }

Help me sorting out this...
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-07-2006, 10:52 AM   #13 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
You can't send multiple files for download - it's just not possible over existing protocols. You'll need to initiate several transfers. You could simply zip up the multiple files and send them through.

As for deleting, does the user running Tomcat have write permissions to those files?
__________________
GitS
Belisarius is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
file types: reading and writing pre_wreck Standard C, C++ 2 09-26-2005 05:49 AM
'old-skool' server code enhancement zyrxfz Standard C, C++ 11 06-14-2005 05:17 AM
.htaccess -- Image Hotlinking Prevention DavH27 HTML / CSS 0 08-27-2004 03:43 AM
.htaccess -- Custom Error Pages v1.1 DavH27 HTML / CSS 0 08-26-2004 04:40 PM
Dynamic File Includes Sarlok ASP Classic 0 02-24-2003 12:49 PM


All times are GMT -8. The time now is 06:54 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle