-
Mar 24th, 2007, 05:03 AM
#1
Managing files
Hi everybody,
i need suggest about managing file in Spring rc application.
A local upload(eg. /localhost/sample.pdf) will be enough,no server upload is needed, because is only a sample application.
How can i "upload"(say select) a file within a form?And how can i "link" that file from my Spring rc app?
I look for it around, but found nothing.
Thanks in advance.
-
Mar 25th, 2007, 03:14 AM
#2
hy Ciccio, you should use Apache HttpClient component,
i've already used for upload zip files.
See following code:
try {
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
//client.getHttpConnectionManager().getParams().setC onnectionTimeout(5000);
// Send any XML file as the body of the POST request
File f = new File(localorderexport +"/"+zipfilename.trim());
System.out.println("File Path = " + f.getAbsolutePath());
System.out.println("File Length = " + f.length());
client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(ui.getCodutente(), ui.getPassword()));
postMethod.setDoAuthentication(true);
// postMethod.setRequestBody(new FileInputStream(f));
//postMethod.getParams().setBooleanParameter(
// HttpMethodParams.USE_EXPECT_CONTINUE,
// true);
Part[] parts = {new FilePart(f.getName(), f, "application/zip" , "ISO-8859-1")};
postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
int statusCode1 = client.executeMethod(postMethod);
if (statusCode1 == HttpStatus.SC_OK) {
} else {
System.out.println("statusLine>>>" + postMethod.getStatusText());
}
postMethod.releaseConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This code upload file, using user and password, via http to specified url that contain http server application CGI, jsp, servlet or other.
i hope this helps you!
Ciao!
Vincenzo!
-
Mar 25th, 2007, 03:24 AM
#3
No, this is not what i need.
I need a lot less that "file upload", and furthermore i need it on a rich-client application, not on a web app.
This is, sort of, what i need:

I need it inside a form, obviously.
I want to save the path of the file in my dbms, and the retrieve the file inside a view by the path retrieved from my db.No need of server upload, only "local" file are needed.
Thanks
-
Apr 7th, 2007, 07:14 PM
#4
I have written a FileBinder you might use:
<code>
import org.apache.log4j.Logger;
import org.springframework.binding.form.FormModel;
import org.springframework.richclient.form.binding.Bindin g;
import org.springframework.richclient.form.binding.suppor t.AbstractBinder;
import org.springframework.richclient.form.binding.suppor t.CustomBinding;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Map;
/**
* Binds a java.io.File to a JComponent
*/
public class FileBinder extends AbstractBinder {
private final Logger log = Logger.getLogger(this.getClass());
protected FileBinder() {
super(File.class);
}
protected JComponent createControl(Map context) {
return new ImageFileComponent();
}
protected Binding doBind(JComponent control, FormModel formModel, String formPropertyPath, Map context) {
final ImageFileComponent filePicker = (ImageFileComponent) control;
return new CustomBinding(formModel, formPropertyPath, File.class) {
/**
* Called when the underlying property's value model value changes.
*/
protected void valueModelChanged(Object newValue) {
// the user has selected a new file, update the control
filePicker.setFile((File) newValue);
}
protected JComponent doBindControl() {
filePicker.setFile((File) getValue());
filePicker.addPropertyChangeListener("value", new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
controlValueChanged(filePicker.getFile());
}
});
return filePicker;
}
/**
* Called when the read only state of the bound property changes.
*/
protected void readOnlyChanged() {
filePicker.setEnabled(isEnabled() && !isReadOnly());
}
/**
* Called when the enabled state of the bound property changes.
*/
protected void enabledChanged() {
filePicker.setEnabled(isEnabled() && !isReadOnly());
}
};
}
private static class ImageFileComponent extends JPanel implements ActionListener {
JLabel label;
JButton button;
final JFileChooser fc = new JFileChooser();
private File file;
public ImageFileComponent() {
label = new JLabel("");
button = new JButton("Choose file...");
button.addActionListener(this);
this.add(label);
this.add(button);
}
public void setFile(File file) {
this.file = file;
updateLabel();
}
private void updateLabel() {
if (file != null) {
label.setText(file.getName());
} else {
label.setText("");
}
}
public File getFile() {
return file;
}
public void setEnabled(boolean enabled) {
}
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File oldFile = file;
file = fc.getSelectedFile();
updateLabel();
firePropertyChange("value", oldFile, file);
}
}
}
}
</code>
-
Apr 8th, 2007, 03:55 AM
#5
Thanks for interesting.I'll try to use it....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules