How to display text file in JSP?
Hi,
I am new to servlet/JSP/java programming.
I have been able to download a file using ftp. I dont know who to display it in JSP.
Here is my code. Any help will be appreciated...
<%@ page import="java.util.*" %>
<%@ page import="java.lang.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%@ page import="com.dmhistory.*" %>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<%
byte[] bytes = new byte[4096];
URL theUrl = new URL("ftp://user:passwd@host/migr/Movers_for_Ora8/BG/logs/FLD-PAC3-20020311.log");
BufferedInputStream in = new BufferedInputStream(theUrl.openStream());
BufferedOutputStream outstream = new BufferedOutputStream(new FileOutputStream( new File( "D:/temp/log.txt" )));
int pos = in.read( bytes );
while ( pos != -1) {
outstream.write( bytes, 0, pos );
pos = in.read( bytes );
%>
<%
in.close();
%>
DONE!!!
</body>
</html>
How can i display this downloaded file in the browser?
I want to display it in the original format i.e which new line and carriage returns...line by line..
Thanks,
Hi,
I finally go it to display. but now I have new problem. The browser shows the text without the /n/r.
But when I view the source using 'view->source', if see that the text has got proper format with newline and carriage returns.
Any ideas how to display it correctly in the browser?
<%
URL theUrl = new URL("ftp://user:pwd@host/migr/Movers_for_Ora8/BG/logs/FLD-PAC3-20020311.log");
//URL theUrl = new URL("ftp://dbcmaint:[email protected]/migr/Movers_for_Ora8/SNP/logs/SNP-PAC2-20020314.log");
URLConnection uc = theUrl.openConnection();
InputStream content = uc.getInputStream();
BufferedInputStream in = new BufferedInputStream (content);
LineNumberReader lnr = new LineNumberReader (new InputStreamReader (in));
String tempStr = "";
String str = "";
while ((tempStr = lnr.readLine()) != null) {
str += (tempStr);
%>
<%=tempStr%>
<%tempStr = "";
%>
<%
in.close();
lnr.close();
%>
Thanks
Similar Messages
-
Hi all,
I can call html or jsp file from jsp file, but how can I call text file in jsp, I put my text file same lever asmy jsp file it don't work!
where I should put the text file??
thank youmary,
since you knew the file name ,when clicked in name send to server,read the file and write to servlet outputstream.
I think this would help you.
If anything wrong in mycode ..forums will help you further
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
int bytesRead=0;
byte buff[]=new byte[1024];
File f=new File(test.txt);
try{
bis= new BufferedInputStream(new FileInputStream(f));
bytesRead=bis.read(buff,0,buff.length);
if(bytesRead!=-1){
// create a BufferedOutputStream from ServletOutputStream
bos=new BufferedInputStream(response.getOutputStream());
do{
bos.write(buff,0,bytesRead);
}while((bytesRead=bis.read(buff,0,buff.length))!=-1)
}catch(Exception e){
////error handling
} -
How to display text file text in aTextArea component
Hello
ive managed to display the text file text in the console, but i want it to show up in the textArea component i have in a frame,
it seems to display the last character . in the text area
wheres the rest of the text...this was a lot easier in VB.NET
any help would be much appreciated
heres my code...
private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
// TODO add your handling code here:
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files Only","txt");
editorFileChooser.setFileFilter(filter);
int returnVal = editorFileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION){
System.out.println("You chose to open this file: " + editorFileChooser.getSelectedFile().getName());
openFileName = editorFileChooser.getSelectedFile().getName();
openFile = new File(openFileName);
try {
this.readFile = new FileReader(openFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
int inputCharacters;
try {
while ((inputCharacters = readFile.read()) != -1){
System.out.print((char)inputCharacters);
char[]CHARAARAY = {(char)inputCharacters};
s3 = new String(CHARAARAY );
this.editorTextArea.setLineWrap(true);
this.editorTextArea.setText(s3.toString());
readFile.close();
catch(IOException ex){
System.out.println();
}//GEN-LAST:event_openButtonActionPerformed>
wheres the rest of the text...this was a lot easier in VB.NET>If you only need to code for Windows, use VB. For code that works on computers, learn the Java idioms, especially with regard to X-plat issues with Files.
>
heres my code...>That was a code snippet. You will get better chance of help around these parts, if you can prepare an SSCCE. The code below* is an SSCCE.
There was a basic problem in the logic of that code, that resulted in the assumption (by the JRE) that every file the user attempted to load, was located in the 'current directory'. It was fixed by abandoning the String based file name of the file, and instead setting openFile to the selected file in the chooser. I also demolished the loading code by replacing the JTextArea with a JEditorPane (much easier).
* Altered code;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.*;
class TestLoadFile extends JPanel {
JFileChooser editorFileChooser;
File openFile;
JEditorPane editorTextArea;
TestLoadFile() {
super(new BorderLayout());
editorTextArea = new JEditorPane();
add(
new JScrollPane(editorTextArea),
BorderLayout.CENTER );
JButton load = new JButton("Load File");
load.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
openButtonActionPerformed(ae);
add( load, BorderLayout.SOUTH );
editorFileChooser = new JFileChooser();
public static void main(String[] args) {
TestLoadFile tlf = new TestLoadFile();
JOptionPane.showMessageDialog(null, tlf);
Dimension preferredSize = new Dimension(600,400);
public Dimension getPreferredSize() {
return preferredSize;
private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {
//GEN-FIRST:event_openButtonActionPerformed
// TODO add your handling code here:
FileNameExtensionFilter filter = new
FileNameExtensionFilter("Text Files Only","txt");
editorFileChooser.setFileFilter(filter);
int returnVal = editorFileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION){
System.out.println("You chose to open this file: " +
editorFileChooser.getSelectedFile().getName());
// wrong!
//openFileName = editorFileChooser.getSelectedFile().getName();
//openFile = new File(openFileName);
// right!
openFile = editorFileChooser.getSelectedFile();
try {
editorTextArea.setPage(openFile.toURI().toURL());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//GEN-LAST:event_openButtonActionPerfor
} -
How to display text file in swing
hello,
I have screen where user enters batch number and number.
when key is pressed a list is displayed on the screen.how to do this and how to relate my actionlistener with the function that generates the list...Hi,
It is simple. Just create a button and add it to the action listener as button.addActionListener(this);
Your class should extend ActionListener. and also there should be a method named - actionPerformed(ActionEvent avt) in your class
Now inside this method, just compare the action as:
if(avt.getSource()==button)
// in this method, hjust take your file contents and display it on the same window, or create a new Frame here and display it in that window.
If you want to display the file contents in the same window, then do the following.
Declare a public JLabel variable for the class and create the JLabel along with the other components as usual but without giving any values. Now in the action part of the button, read the file contents in a string variable, and set the string to the jlabel as folows:
label.setText(string);
Hope it helps,
Cochu. -
Display uploaded file on JSP page
Hi All,
I want to display uploaded file in JSP.
Initially I am displaying File Name after saving the file & click on edit link.
i.e on JSP Page I have File Upload component & save button.With that save button I am saving my file to databse.
So when I click on edit link I am getting that file name from Databse then I am displaying it.
But now I want to display uploaded file name on JSP page before saving to databse.i.e I will have browse & Upload button.When I click on browse button I will open window from where I will select file.
This is working fine as,<x:inputFileUpload id="uploadfile" value="#{errorLotBean.file}" storage="file"></x:inputFileUpload>
But when I click on upload button that uploaded file should be displyed there only.Can anyone please tell me how to do this ?
Thanks
SandipThanks a lot Siefert,
I tried the way mentioned in URL.
But what if I want to display all uploaded file on my screen.
i.e. If user click on browse & select File A then click on upload button.
(Here the File A will be displayed) with code
<h:outputtext value="#{benaName.file.name}"
But what if after displaying File A if user decide to upload another file File B.
How to display that ?
with <h:outputtext value="#{benaName.file.name}" its dispalying one file only.
Thanks
Sandip -
How to display RTF file and .doc file in JTextPane
How to display RTF file and .doc file in JTextPane??
Duplicate post
http://forum.java.sun.com/thread.jsp?forum=57&thread=567029&tstart=0&trange=30 -
How to display Pdf file in iPad site
Hi
How to display Pdf file in web page which can able to view in iPad safari?
Thanks,
ArunYou can't really.
You need to use DHTML in the swf-wrapper HTML file, usually a
division wrapped iFrame, and load the PDF into the iFrame as an
overlay to the Flash. -
How to display text on last but one page in SAPSCRIPTS
how to display text on last but one page in SAPSCRIPTS
u have create one Foooter window , this has to be called in only One Page.So hardcode /assign this window to only one PAGE number.
regards
Prabhu -
How to display Doc file format in the JEditPane?
At this time , JEditPane supports RTF and HTML file format ,but
Could anyone tell me how to display DOC file format using JEditPane
or other JTextComponents?
Thx a lot!
CatonHi,
there is no support for doc files (M$ Word?) in Java. You would have to create your own classes for that eventually subclassing JEditorPane, EditorKit, Document etc. As well you'd need your own reader for the doc format.
The problem I see with that however is that there is nothing such as thedoc file format. Microsoft just saves anything produced by MS Word into files ending with .doc. But, if there is anything such as a .doc file format, it changes more often than the weather in April.
Ulrich -
How to display the file status in the status bar?
Hi all,
Can anyone tell me how to display the file status in the status bar?
The file status can consists: the type of the file, the size of the file etc..
thanx alot..class StatusBar extends JComponent {
JLabel l = new JLabel("ready");
public StatusBar() {
super();
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(l);
public void setText( String prompt ) {
l.setText(prompt);
setVisible( true );
public void paint(Graphics g) {
super.paint(g);
} -
How to display texts automatic. besides entered value for a field in Trans.
How to display texts automatically besides the entered value for a field in a standard transaction screen. For example you have a value table and a text table associated to it. Then on entering the value field and pressing enter the text associated should get displayed immediately besides the value. Like if you have 'LOC' as the value and 'Location' as the text associated to it, on entering this value 'LOC', you automatically get the text 'Location' printed besides it automatically in a transaction screen ?
Message was edited by: Sarika KediaHi sarika,
Welcome to SDN.
1. first of all, such display of text,
is not automatic.
(it appears to be automatic)
2. At design time,
a) take one extra field for text
and mark it as OUTPUT ONLY
3. Then in PBO coding,
call some module, and in that module
write code
4. The code should be to
select from TEXT Table
into the work area.
EG. THE SCREEN TEXT FIELD NAME IS
T510A-FIELDNAME.
CLEAR t510a.
SELECT SINGLE * FROM t510a INTO t510a
WHERE trfar = FIELVALUE.
5. This will take care of
displaying the text value of that field.
regards,
amit m. -
How to display flash file in adf pages
how to display flash file in adf pages need help
Thanks all,
It is resolved.
the code i am using to display a flash is below.
<f:verbatim>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version
=10,0,0,0" width="300" height="300" id="11gR1_aniH_grey" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="wmode" value="transparent" />
<param name="movie" value="mx2004demo.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#4d5c64" />
<embed src="../Images/mx2004demo.swf" quality="high" width="300" height="300" name="mx2004demo.swf" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false"
type="application/x-shockwave-flash" wmode="transparent"
pluginspage="http://www.adobe.com/go/getflashplayer" />
</object>
</f:verbatim> -
How to write text file in Shockwave?
Does anybody know how to write text file in Shockwave to
user's disk?
Thanks in advance.Those Xtras can wreak to much havoc when used with the wrong
intent.
What you can do is write with setpref and store a list of
saves and the
saves itself seperatly. Then you'd have to build your own
save/open
dialog to let the user:
* pick a previously saved file to load or overwrite
* have the user type the name of a new file to save.
Only thing that remains is that the user cannot decide where
the files
are saved.
Manno
SiuLinda wrote:
> Thanks a lot for your reply.
> Yes, cookies is good but I have to write a program to
save the text file in
> where the user wants, user can open these files later if
they like, like using
> Filextra and Fileio, but I found all of these xtras seem
to be not supported in
> shockwave.
>
Manno Bult
[email protected] -
How to display XSL file to ASPX in dreamweaver
Hi,
Just wondering how to display XSL file in ASPX page?
I normally do it in ASP page by using server behaviour XSLT transformation and bind it to ASP page and this works very well.
Now I am trying to use the same method and using ASPX page this time but this time there is no option from server behaviour where I can bind it to my ASPX.
Can someone help or perhaps let me know where I can find the information how to do it?
Thanks,
Rush OYou mean you want to see the XML source?
You need to replace the characters '<' and '&' with corresponding entities '<' and '&'. You can use replaceAll, but do the ampersands first.
Then I suggest you probably want to put them in a <PRE> block. -
How to read text file line by line...?
how to read text file line by line, but the linefeed is defined by user, return list of string, each line of file is a item of list?
please help me.
Thanks very muchBrynjar wrote:
In Groovy, you would do something like:
linefeed = "\n" //or "\r\n" if the user chose so
lines = new File('pathtofile').text.split("${linefeed}")This is one of the things that has always annoyed me about Sun's sdk, i.e. the lack of easy ways to do things like that. You always end up making your own utilities or use something like Apache's commons.io. Same goes for jdbc and xml - I'll wait for appropriate topics to show how easy that is in Groovy :)I generally agree, but what I really don't like about the Groovy text-file handling niceties: They don't care about encoding/always use the default encoding. And as soon as you want to specify the encoding, it gets a lot more complex (granted, it's still easier than in Java).
Maybe you are looking for
-
How to remove black "blocked plug-in" window that opens with iPhoto. It covers iPhoto and everything under it. I've right and left clicked the black window, restarting the computer, shutting down the computer, but the window always appears when I ope
-
Itunes crashes when Podcasts begin downloading
Every time I start up itunes it crashes after the podcast begin downloading. I have to open task manager to shut down itunes. I have tried creating a different user account, it seems to be a system wide issue. I have tried the suggestions on the a
-
There si any trouble when I input Chinese to the JTextField in Applet.
There si any trouble when I input Chinese to the JTextField in Applet.
-
Having issues with workbook and sharing
Ive used excel for my scheduling for over 10 years or so and sharing it so everyone in the office has access. Since we switched to the new office, now it changes the fonts, colors, size every time you open it. Tied to set a default them but didnt w
-
ORA-01788: CONNECT BY clause required in this query block
My Oracle version is: 10.2.0.4.0 (for both DEV and PROD) Below is the SQL which throws error ORA-01788 on PROD during the execution. I can guarantee that the syntax is right. Also, the odd thing about this is that, it works fine on the DEV which is t