I have a java application that accepts multipart uploads, my problem is that I would like to have an HTML/JSP front end to this instead of it just working on the server. Based on the code that i've provided, what would be the best way to accomplish this. It was a bit confusing to me because I wasn't sure how to bring the fileuploading portion into a html/jsp page. Any suggestions would help.
Thanks much, tone
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ProgressEvent;
import com.amazonaws.services.s3.model.ProgressListener;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;
public class S3TransferProgressSample {
private static AWSCredentials credentials;
private static TransferManager tx;
private static String bucketName;
private JProgressBar pb;
private JFrame frame;
private Upload upload;
private JButton button;
public static void main(String[] args) throws Exception {
AmazonS3 s3 = new AmazonS3Client(credentials = new ClasspathPropertiesFileCredentialsProvider().getCredentials());
Region usWest2 = Region.getRegion(Regions.US_WEST_2);
s3.setRegion(usWest2);
tx = new TransferManager(s3);
bucketName = "s3-upload-sdk-sample-" + credentials.getAWSAccessKeyId().toLowerCase();
new S3TransferProgressSample();
}
public S3TransferProgressSample() throws Exception {
frame = new JFrame("Amazon S3 File Upload");
button = new JButton("Choose File...");
button.addActionListener(new ButtonListener());
pb = new JProgressBar(0, 100);
pb.setStringPainted(true);
frame.setContentPane(createContentPane());
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser();
int showOpenDialog = fileChooser.showOpenDialog(frame);
if (showOpenDialog != JFileChooser.APPROVE_OPTION) return;
createAmazonS3Bucket();
ProgressListener progressListener = new ProgressListener() {
public void progressChanged(ProgressEvent progressEvent) {
if (upload == null) return;
pb.setValue((int)upload.getProgress().getPercentTransfered());
switch (progressEvent.getEventCode()) {
case ProgressEvent.COMPLETED_EVENT_CODE:
pb.setValue(100);
break;
case ProgressEvent.FAILED_EVENT_CODE:
try {
AmazonClientException e = upload.waitForException();
JOptionPane.showMessageDialog(frame,
"Unable to upload file to Amazon S3: " + e.getMessage(),
"Error Uploading File", JOptionPane.ERROR_MESSAGE);
} catch (InterruptedException e) {}
break;
}
}
};
File fileToUpload = fileChooser.getSelectedFile();
PutObjectRequest request = new PutObjectRequest(
bucketName, fileToUpload.getName(), fileToUpload)
.withProgressListener(progressListener);
upload = tx.upload(request);
}
}
private void createAmazonS3Bucket() {
try {
if (tx.getAmazonS3Client().doesBucketExist(bucketName) == false) {
tx.getAmazonS3Client().createBucket(bucketName);
}
} catch (AmazonClientException ace) {
JOptionPane.showMessageDialog(frame, "Unable to create a new Amazon S3 bucket: " + ace.getMessage(),
"Error Creating Bucket", JOptionPane.ERROR_MESSAGE);
}
}
private JPanel createContentPane() {
JPanel panel = new JPanel();
panel.add(button);
panel.add(pb);
JPanel borderPanel = new JPanel();
borderPanel.setLayout(new BorderLayout());
borderPanel.add(panel, BorderLayout.NORTH);
borderPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
return borderPanel;
}
}
Convert java application to jsp/servlet
Depending on what kind of program it is, it could be as easy as taking the class that has the main()
method in it, making it a subclass of HttpServlet
and taking everything that's in the main()
method and putting it in the doGet() method
of the servlet. Then you'd replace all the System.out.println()
statements with statements to print the output in HTML. This would work if it's just a command line program without a GUI (Graphical User Interface)
.
If your program has no GUI then you can stop reading here. If it does have a GUI, then it could be a lot more complicated. Depending on what kind of program it is, you could do one of the following:
1. Put the whole thing in an Applet.
This is pretty simple. Instead of replacing a few lines to turn the program into a Servlet, you'd be replacing a few lines to turn it into an Applet.
This you could do if you don't need the server to communicate with, like if you had a solitare game. Otherwise, you could do #2.
2. Put the GUI in an Applet and keep the back end on the server.
This is what you'd call a "client/server" application.
3. Create a JSP/Servlet based website.
The most complicated option, but if you don't want to use Applets this is the only way to go.Here you're changing from a Java based GUI to an HTML/CSS based GUI. You might also need some JavaScript. If you're not familiar with all this stuff, but you're comfortable making Java GUI's using things like Swing, you might want to check out GWT (Google Web Toolkit).
This allows you to make rich websites using plain Java. The Java code "compiles" into HTML and Javascript.
Another one:
It's done at runtime, when the JSP is invoked for the first time. Some web servers also come with a JSP compiler allowing to do that at build time, which has two advantages :
It allows detecting JSP syntax errors at build time rather than runtime.
It avoids the first invocation time penalty (it takes some time compiling JSP to Java and then Java to bytecode).