File permission
Hej.Jeg har lavet et program som åbner en JFileChooser så man kan markere flere filer, med når jeg har markeret nogle filer og trykker på åben, så brokker den sig over file permission, selvom jeg har lavet et certificat. Jeg har set på http://jupload.sourceforge.net/ at de sagtens kan, kun med et certificat. Min kode er som følgende:
UploaderApplet.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.Border;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import netscape.javascript.JSObject;
/**
* This class is a replica of the UploaderApplet.
* It is because you have to close all browsers of the same kind every time you have made
* a change and want to test it.
*
* @author df
*
*/
public class UploaderApplet extends JApplet
{
/**
*
*/
private JSObject jso;
private static final long serialVersionUID = 6985387794087346405L;
private static JPanel container = new JPanel();
private static JPanel list = new JPanel();
private static Dimension size = new Dimension(640, 480); // The size of the frame, including the systems theme
private static JFileChooser chooser = new JFileChooser(); // The file chooser, that lets people select there files
private static File[] listOfFiles; // The list of files the user has selected
private static Integer currentFile = null;
private static String postUrl = "http://localhost/java/upload.php";
/**
* This is the main method that is runned as the first method
* It places the buttons and takes care of adding the window
*
* @param args
*/
public void init ()
{
Border emptyBorder = BorderFactory.createEmptyBorder();
chooser.setMultiSelectionEnabled(true);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
container.setSize(new Dimension(620, 350));
container.setBorder(emptyBorder);
container.setPreferredSize(new Dimension(620, 350));
list.setLocation(10, 0);
list.setSize(new Dimension(600, 350));
list.setPreferredSize(new Dimension(600, 350));
list.setBackground(Color.white);
createList();
JPanel buttons = new JPanel();
buttons.setSize(new Dimension(600, 300));
buttons.setLocation(40, 40);
JButton btn1 = new JButton("Browse");
btn1.setActionCommand("browse");
JButton btn2 = new JButton("Upload");
btn2.setActionCommand("upload");
buttons.add(btn1);
buttons.add(btn2);
container.add(buttons);
container.add(list);
createAndShow();
add(container);
jso = JSObject.getWindow (this);
String[] args = new String[1];
args[0] = "Started";
jso.call("fromJava", args);
/*jso = JSObject.getWindow (this);
jso.eval("fromJava('fsi');");
String[] args = new String[1];
args[0] = "Seconds";
jso.call("fromJava", args);*/
}
private void createAndShow()
{
//add(container);
}
/**
* Creates a row in the list,
* so that the user can see what is what
*/
private static void createList ()
{
Font font = new Font("Lucida Sans Regular ", Font.PLAIN, 11);
Color background = new Color(222, 230, 235);
Dimension iD = new Dimension(30, 15);
Dimension nD = new Dimension(130, 15);
Dimension pD = new Dimension(340, 15);
Dimension sD = new Dimension(50, 15);
JPanel row = new JPanel();
row.setBackground(background);
Border border = BorderFactory.createEmptyBorder(); // no border
JTextField i = new JTextField("Nr:");
i.setEditable(false);
i.setFont(font);
i.setBackground(background);
i.setSize(iD);
i.setPreferredSize(iD);
i.setBorder(border); // the borders are ugly, so disable them
JTextField n = new JTextField("Name:");
n.setFont(font);
n.setBackground(background);
n.setSize(nD);
n.setPreferredSize(nD);
n.setBorder(border); // the borders are ugly, so disable them
n.setEditable(false);
JTextField p = new JTextField("Path:");
p.setFont(font);
p.setBackground(background);
p.setSize(pD);
p.setPreferredSize(pD);
p.setBorder(border); // the borders are ugly, so disable them
p.setEditable(false);
JTextField s = new JTextField("Size:");
s.setFont(font);
s.setBackground(background);
s.setSize(sD);
s.setPreferredSize(sD);
s.setBorder(border); // the borders are ugly, so disable them
s.setEditable(false);
row.add(i);
row.add(n);
row.add(p);
row.add(s);
list.add(row);
}
public static void addRow (int i, String n, String p)
{
Font font = new Font("Lucida Sans Regular ", Font.PLAIN, 11);
Color background;
if (i%2 == 1) {
background = new Color(222, 230, 235);
} else {
background = new Color(237, 244, 249);
}
//System.out.println(i + " " + n + " " + p);
Dimension iD = new Dimension(30, 15);
Dimension nD = new Dimension(130, 15);
Dimension pD = new Dimension(340, 15);
Dimension sD = new Dimension(50, 15);
JPanel row = new JPanel();
row.setBackground(background);
Border border = BorderFactory.createEmptyBorder(); // no border
JTextField nr = new JTextField(new Integer(i).toString());
nr.setFont(font);
nr.setBackground(background);
nr.setSize(iD);
nr.setPreferredSize(iD);
nr.setBorder(border); // the borders are ugly, so disable them
nr.setEditable(false);
JTextField name = new JTextField(n);
name.setFont(font);
name.setBackground(background);
name.setSize(nD);
name.setPreferredSize(nD);
name.setBorder(border); // the borders are ugly, so disable them
name.setEditable(false);
JTextField path = new JTextField(p);
path.setFont(font);
path.setBackground(background);
path.setSize(pD);
path.setPreferredSize(pD);
path.setBorder(border); // the borders are ugly, so disable them
path.setEditable(false);
File file = new File(p);
long longSize = file.length();
String strSize = getSize(longSize);
JTextField size = new JTextField(strSize);
size.setFont(font);
size.setBackground(background);
size.setSize(sD);
size.setPreferredSize(sD);
size.setBorder(border); // the borders are ugly, so disable them
size.setEditable(false);
row.add(nr);
row.add(name);
row.add(path);
row.add(size);
list.add(row);
list.revalidate();
}
/**
* Is activated when the user clicks on the browse button.
* It will show a window where you can select some files
*
*/
public void browse ()
{
JFrame f = new JFrame("asd");
int returnVal = chooser.showOpenDialog(f);
//File f = null;
if (returnVal == JFileChooser.APPROVE_OPTION) {
//f = chooser.getSelectedFile();
listOfFiles = chooser.getSelectedFiles();
displayFiles();
}
String[] args = new String[1];
args[0] = "Browsed";
jso.call("fromJava", args);
String[] args1 = new String[1];
args1[0] = listOfFiles.toString();
jso.call("fromJava", args1);
}
/**
* This will loop through the list of selected files
* and then display some relevant informations,
* by adding them to a JPanel that then is added to the window
*/
private void displayFiles ()
{
for (int i = 0; i < listOfFiles.length; i++) {
if (i >= 10) continue;
File f = listOfFiles[i];
String name = f.getName();
String path = f.getAbsolutePath();
addRow(i+1, name, path);
}
String[] args = new String[1];
args[0] = "Added files";
jso.call("fromJava", args);
}
/**
* Is called every time we want to upload a file.
* This is automaticly done when it is the first time a file should be uploaded
* and for each time a file has been uploaded, this method will be called, and there is more files
*/
public void uploadNext ()
{
if (currentFile == null) {
currentFile = 0;
} else {
currentFile++;
}
String[] args = new String[1];
args[0] = "uploadNext";
jso.call("fromJava", args);
String[] args1 = new String[1];
args1[0] = new Integer(currentFile).toString();
jso.call("fromJava", args1);
if (currentFile < 10 && currentFile < listOfFiles.length) {
String[] args2 = new String[1];
args2[0] = "Start upload";
jso.call("fromJava", args2);
System.out.println(currentFile);
File file = listOfFiles[currentFile];
String source = file.getAbsolutePath();
try {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(postUrl);
method.setRequestHeader("User-Agent", "ascii");
Part[] parts = {
new StringPart("action", "uploadfile"),
new StringPart("project", "lib"),
new FilePart(source, file)
};
method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
int statusCode = client.executeMethod(method);
if (statusCode == 200) {
String[] args3 = new String[1];
args3[0] = "Uploaded";
jso.call("fromJava", args3);
//System.out.println("asd");
upload();
} else if( statusCode != -1 )
{
String[] args4 = new String[1];
args4[0] = "Could not upload";
jso.call("fromJava", args4);
String contents = method.getResponseBodyAsString();
method.releaseConnection();
File f = new File("log.txt");
FileWriter fw = new FileWriter(f);
fw.write(contents);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Called only once, but will take care of uploading all the files in the list
*/
public void upload ()
{
uploadNext();
}
private static String getSize (long size)
{
String[] types = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB"};
double exp = 0;
double converted_value = 0;
if (size > 0) {
exp = Math.floor( Math.log(size)/Math.log(1024) );
converted_value = Math.round( size / Math.pow(1024, Math.floor(exp)) );
}
int value = (int) converted_value;
String str = new Integer(value).toString();
int expInt = (int) exp;
return str + " " + types[expInt];
}
}
makekey.bat
keytool -genkey -keyalg rsa -alias THEKEY
export.bat
keytool -export -alias THEKEY -file CERTIFICAT.crt
generate.bat
javac -cp commons.jar;netscape.jar UploaderApplet.java
jar cvf UploaderApplet.jar UploaderApplet.class commons.jar netscape.jar
jarsigner LocalFiler.jar THEKEY
Men med disse kan jeg ikke få lov til at at vælge flere filer.
Er der nogen der kan fortælle mig hvad jeg gør forkert eller mangler?
Jeg laver det i java 6