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

x
?
Solved

Download and save a file

Posted on 2003-03-12
10
Medium Priority
?
152 Views
Last Modified: 2010-03-31
Is anyone know how to download part of a given file? Some of those log files are too big to download. The user only wants to view and download recent entries.

public File downLoadFile(File file, int numLine){
...
}

Thanks for your help in advance!
0
Comment
Question by:DevelHelper
10 Comments
 
LVL 1

Expert Comment

by:sct75
ID: 8122307
Couple of following strategies could be considered, but seems it all needs some server side work, assuming the log files are saved on the server side.

1) Send the download request to a Servlet that turns around to fetch the recent entries from the log file, create a temp file then return such file as download request; or just return the recent entries from the log file directly as html output;

2) Depends on which kind app or web server u use. U could consider to write a service that will regularly (say every 10 minutes) fetch most recent entries from the log file and put them into a new file. Whenever the download request comes, you could just grab such file and return.

0
 

Author Comment

by:DevelHelper
ID: 8122485
Thanks for your answer.
The log files are on server side. What I'm goint to do is that to write a function returning the log file and store the file in a EJB sessionless bean. Then the servlet read the file. But now I don't know the detail of the code.
0
 
LVL 1

Expert Comment

by:sct75
ID: 8122765
What do u mean by saying "Then the servlet read the file. But now I don't know the detail of the code."?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:simonletter
ID: 8125123
client side work is possible with HTTP/1.1:
- telnet localhost 80
- GET /index.html HTTP/1.1
- Host: localhost
- Range: bytes=10-20
- <Enter>

bytes 10 to 20 are downloaded. This works for Apache not Tomcat.

You need to translate this to Java though which I haven't done before. And figure out which bytes to download in the log file.

0
 
LVL 1

Accepted Solution

by:
ayeen earned 200 total points
ID: 8125278
import java.util.*;
import java.io.*;



//Description: This method is for reading and cutting of files.
//This would display the latest n lines.
//Parameters: file - File to be read.This should be of File type. numLines - the n number of lines you want to display


public StringBuffer readAndCutFiles(File file, int numLines){

 StringBuffer display_line = new StringBuffer();

 try
 {
     String line = "";
     long number = 0;
     long cursor = 0;

     BufferedReader count = new BufferedReader(new FileReader(file));
     BufferedReader in = new BufferedReader(new FileReader(file));

          while ((count.readLine())!=null)
          {++number;}
          count.close();

     if (number>numLines)
     {
          for (cursor = 0; cursor<number-numLines; cursor++){
               in.readLine();
          }
          while ((line=in.readLine())!=null){
               display_line.append(line);
               display_line.append("\n");
          }
     }else{
          while ((line=in.readLine())!=null){
               display_line.append(line);
               display_line.append("\n");
          }
     }

  }catch(Exception ex){
          System.out.println("Exception is: " + ex);
  }

  return display_line;
}


goodluck!

:)
0
 

Author Comment

by:DevelHelper
ID: 8128295
I wanted to test the solution provided by Ayeen, but I don't know how to write a test code to read the StringBuffer returned from readAndCutFiles(). Could you help this?
0
 

Author Comment

by:DevelHelper
ID: 8128658
I wrote the following test code for Ayeen's solution, but the output file format is not the same as source file. Do you know how to fix this problem?

public static void main(String[] args) throws IOException{
       
       FLVDownLoad f=new FLVDownLoad();
       
       StringBuffer newCutFile;
       
       newCutFile=f.readAndCutFiles(new File("C:\\var\\websphere4\\logs\\ArtSecurityLog.log"),3);
     
      try{
                File file=new File("C:\\var\\websphere4\\logs\\tempLogFile.log");

            FileOutputStream outstr=new FileOutputStream(file);

            ObjectOutputStream outStream   = new ObjectOutputStream(outstr);

            outStream.writeObject(newCutFile);
           
      }catch (IOException e) {

            System.out.println("File write error: " + e.toString());

      }
           
    }
   
0
 
LVL 1

Expert Comment

by:sct75
ID: 8128799
To DevelHelper,

In your test program, please do not use ObjectOutputStream as the output stream, but instead, using BufferedWriter or some other character output approach; otherwise, what you tring to do is to output the whole FLVDownLoad object to a file rather than the 3 lines of logs you expects. Ayeen's code gives you the sense of how to read in the file with certain number of lines, but you need to finish the other part of the program to write them out to a given output file or output stream. You could do so by extending the parameter list of his class to take in the output approach (either a file or output stream or writer) as parameter.

If you need further help, please let Ayeen or me know.
0
 

Author Comment

by:DevelHelper
ID: 8129198
Thank you! sct75. I don't know which is the best solution for my case. What I'm tring to do is that read file from remote file system and then the servlet write the file to Internet Explorer or any editor. Please show me your best solution here. I will give you 80 points. But I think I should give this 50 points to Ayeen.
0
 
LVL 1

Expert Comment

by:sct75
ID: 8132543
DevelHelper,

Please follow the link below to find the answer to a similar question as yours.

http://www.experts-exchange.com/Programming/Q_20538300.html

If you need further explanation or help, please let me know or you could post another question to me here.

Good luck.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

578 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