?
Solved

Download and save a file

Posted on 2003-03-12
10
Medium Priority
?
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 Month13 days, 4 hours left to enroll

777 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