# 10 Re: Location of File
Unless you have your own server it is not easy to find a web host that supports servlets. Maybe I'm out of line here (this being a java forum) but with php you can have this done easily. Here is the php code:
<?php
/**
** Get contents of a file in the http://<hostname>/docs/ directory
** and return the contents to the client
**/
$fileName=$_REQUEST['file_name'];
$fPath=$_SERVER['DOCUMENT_ROOT']."/docs/".$fileName;
$fp=fopen($fPath,"r");
$content=fread($fp,filesize($fPath));
fclose($fp);
echo $content;
?>
here is the applet code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class GetFileApplet extends JApplet implements ActionListener {
private boolean isStandalone = false;
JPanel jPanel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JTextField filenameTF = new JTextField();
JButton getFileBtn = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea repTA = new JTextArea();
JButton clearBtn = new JButton();
GridBagLayout gridBagLayout1 = new GridBagLayout();
public GetFileApplet() {
}
public void init() {
try {
jbInit();
clearBtn.addActionListener(this);
getFileBtn.addActionListener(this);
}
catch(Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==getFileBtn) {
String s = null;
try {
s = getContents().toString();
repTA.append(s);
}
catch (Exception ex) {
repTA.append("\n****************** ERROR ******************");
repTA.append("\nGetting file: "+filenameTF.getText().trim()+" failed: ");
repTA.append("\n"+ex.getMessage());
ex.printStackTrace();
}
} else if (e.getSource()==clearBtn) {
repTA.setText("");
}
}
/**
* Get server side file
*/
public StringBuffer getContents() throws Exception {
StringBuffer buffer;
String line;
int responseCode;
HttpURLConnection connection;
BufferedReader dataInput;
String hostName=getDocumentBase().getHost();
String p=getDocumentBase().getPath();
String fileName=filenameTF.getText().trim();
int ix = p.lastIndexOf("/");
String pth = "http://" + hostName + "/getFile.php?file_name="+fileName;
URL url = new URL(pth);
connection = (HttpURLConnection)url.openConnection();
responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new Exception("HTTP response code: " +
String.valueOf(responseCode));
}
try {
buffer = new StringBuffer();
InputStream input = connection.getInputStream();
dataInput = new BufferedReader(new InputStreamReader(input));
while ( (line = dataInput.readLine()) != null) {
buffer.append(line);
buffer.append('\n');
//System.out.println("line: " + line);
}
input.close();
} catch (Exception ex) {
ex.printStackTrace(System.err);
return null;
}
return buffer;
}
//Component initialization
private void jbInit() throws Exception {
this.setSize(new Dimension(400,300));
jPanel1.setLayout(gridBagLayout1);
jLabel1.setText("File Name");
getFileBtn.setText("Get File");
filenameTF.setText("");
repTA.setMargin(new Insets(2, 2, 2, 2));
repTA.setText("");
clearBtn.setText("Clear");
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(8, 16, 0, 0), 3, 1));
jPanel1.add(filenameTF, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(8, 8, 0, 0), 171, -5));
jPanel1.add(getFileBtn, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(8, 5, 0, 8), 35, -7));
jPanel1.add(jScrollPane1, new GridBagConstraints(0, 1, 3, 1, 1.0, 1.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(14, 16, 0, 14), 360, 193));
jScrollPane1.getViewport().add(repTA, null);
jPanel1.add(clearBtn, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(6, 16, 7, 137), 43, -7));
}
}
and here is the html.
<html>
<head>
<title>
HTML Test Page
</title>
</head>
<body>
Getting a file from the server<br>
<applet
codebase = "."
code = "GetFileApplet.class"
name = "getFile"
width = "400"
height = "300"
hspace = "0"
vspace = "0"
align = "middle"
>
</applet>
</body>
</html>
sjalle at 2007-11-11 22:46:52 >
