Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f05e269

Browse files
committed
Sample project for Java Swing HTTP File Downloader
0 parents  commit f05e269

15 files changed

+480
-0
lines changed

Compile instructions.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
javac -d build\classes -cp src src\net\codejava\swing\download\SwingFileDownloadHTTP.java
2+
jar cfev SwingFileDownloadHTTP.jar net.codejava.swing.download.SwingFileDownloadHTTP -C build\classes .

SwingFileDownloadHTTP.jar

10.2 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package net.codejava.swing;
2+
3+
import java.io.File;
4+
import javax.swing.filechooser.FileFilter;
5+
6+
public class FileTypeFilter extends FileFilter {
7+
8+
private String extension;
9+
private String description;
10+
11+
public FileTypeFilter(String extension, String description) {
12+
this.extension = extension;
13+
this.description = description;
14+
}
15+
16+
@Override
17+
public boolean accept(File file) {
18+
if (file.isDirectory()) {
19+
return true;
20+
}
21+
return file.getName().toLowerCase().endsWith(extension);
22+
}
23+
24+
public String getDescription() {
25+
return description + String.format(" (*%s)", extension);
26+
}
27+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package net.codejava.swing;
2+
3+
import java.awt.FlowLayout;
4+
import java.awt.event.ActionEvent;
5+
import java.awt.event.ActionListener;
6+
7+
import javax.swing.JButton;
8+
import javax.swing.JFileChooser;
9+
import javax.swing.JLabel;
10+
import javax.swing.JPanel;
11+
import javax.swing.JTextField;
12+
13+
public class JFilePicker extends JPanel {
14+
private String textFieldLabel;
15+
private String buttonLabel;
16+
17+
private JLabel label;
18+
private JTextField textField;
19+
private JButton button;
20+
21+
private JFileChooser fileChooser;
22+
23+
private int mode;
24+
public static final int MODE_OPEN = 1;
25+
public static final int MODE_SAVE = 2;
26+
27+
public JFilePicker(String textFieldLabel, String buttonLabel) {
28+
this.textFieldLabel = textFieldLabel;
29+
this.buttonLabel = buttonLabel;
30+
31+
fileChooser = new JFileChooser();
32+
33+
setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
34+
35+
// creates the GUI
36+
label = new JLabel(textFieldLabel);
37+
38+
textField = new JTextField(30);
39+
button = new JButton(buttonLabel);
40+
41+
button.addActionListener(new ActionListener() {
42+
@Override
43+
public void actionPerformed(ActionEvent evt) {
44+
buttonActionPerformed(evt);
45+
}
46+
});
47+
48+
add(label);
49+
add(textField);
50+
add(button);
51+
52+
}
53+
54+
private void buttonActionPerformed(ActionEvent evt) {
55+
if (mode == MODE_OPEN) {
56+
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
57+
textField.setText(fileChooser.getSelectedFile().getAbsolutePath());
58+
}
59+
} else if (mode == MODE_SAVE) {
60+
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
61+
textField.setText(fileChooser.getSelectedFile().getAbsolutePath());
62+
}
63+
}
64+
}
65+
66+
public void addFileTypeFilter(String extension, String description) {
67+
FileTypeFilter filter = new FileTypeFilter(extension, description);
68+
fileChooser.addChoosableFileFilter(filter);
69+
}
70+
71+
public void setMode(int mode) {
72+
this.mode = mode;
73+
}
74+
75+
public String getSelectedFilePath() {
76+
return textField.getText();
77+
}
78+
79+
public JFileChooser getFileChooser() {
80+
return this.fileChooser;
81+
}
82+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package net.codejava.swing.download;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
8+
import javax.swing.JOptionPane;
9+
import javax.swing.SwingWorker;
10+
11+
/**
12+
* Execute file download in a background thread and update the progress.
13+
* @author www.codejava.net
14+
*
15+
*/
16+
public class DownloadTask extends SwingWorker<Void, Void> {
17+
private static final int BUFFER_SIZE = 4096;
18+
private String downloadURL;
19+
private String saveDirectory;
20+
private SwingFileDownloadHTTP gui;
21+
22+
public DownloadTask(SwingFileDownloadHTTP gui, String downloadURL, String saveDirectory) {
23+
this.gui = gui;
24+
this.downloadURL = downloadURL;
25+
this.saveDirectory = saveDirectory;
26+
}
27+
28+
/**
29+
* Executed in background thread
30+
*/
31+
@Override
32+
protected Void doInBackground() throws Exception {
33+
try {
34+
HTTPDownloadUtil util = new HTTPDownloadUtil();
35+
util.downloadFile(downloadURL);
36+
37+
// set file information on the GUI
38+
gui.setFileInfo(util.getFileName(), util.getContentLength());
39+
40+
String saveFilePath = saveDirectory + File.separator + util.getFileName();
41+
42+
InputStream inputStream = util.getInputStream();
43+
// opens an output stream to save into file
44+
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
45+
46+
byte[] buffer = new byte[BUFFER_SIZE];
47+
int bytesRead = -1;
48+
long totalBytesRead = 0;
49+
int percentCompleted = 0;
50+
long fileSize = util.getContentLength();
51+
52+
while ((bytesRead = inputStream.read(buffer)) != -1) {
53+
outputStream.write(buffer, 0, bytesRead);
54+
totalBytesRead += bytesRead;
55+
percentCompleted = (int) (totalBytesRead * 100 / fileSize);
56+
57+
setProgress(percentCompleted);
58+
}
59+
60+
outputStream.close();
61+
62+
util.disconnect();
63+
} catch (IOException ex) {
64+
JOptionPane.showMessageDialog(gui, "Error downloading file: " + ex.getMessage(),
65+
"Error", JOptionPane.ERROR_MESSAGE);
66+
ex.printStackTrace();
67+
setProgress(0);
68+
cancel(true);
69+
}
70+
return null;
71+
}
72+
73+
/**
74+
* Executed in Swing's event dispatching thread
75+
*/
76+
@Override
77+
protected void done() {
78+
if (!isCancelled()) {
79+
JOptionPane.showMessageDialog(gui,
80+
"File has been downloaded successfully!", "Message",
81+
JOptionPane.INFORMATION_MESSAGE);
82+
}
83+
}
84+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package net.codejava.swing.download;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.net.HttpURLConnection;
6+
import java.net.URL;
7+
8+
/**
9+
* A utility that downloads a file from a URL.
10+
*
11+
* @author www.codejava.net
12+
*
13+
*/
14+
public class HTTPDownloadUtil {
15+
16+
private HttpURLConnection httpConn;
17+
18+
/**
19+
* hold input stream of HttpURLConnection
20+
*/
21+
private InputStream inputStream;
22+
23+
private String fileName;
24+
private int contentLength;
25+
26+
/**
27+
* Downloads a file from a URL
28+
*
29+
* @param fileURL
30+
* HTTP URL of the file to be downloaded
31+
* @throws IOException
32+
*/
33+
public void downloadFile(String fileURL) throws IOException {
34+
URL url = new URL(fileURL);
35+
httpConn = (HttpURLConnection) url.openConnection();
36+
int responseCode = httpConn.getResponseCode();
37+
38+
// always check HTTP response code first
39+
if (responseCode == HttpURLConnection.HTTP_OK) {
40+
String disposition = httpConn.getHeaderField("Content-Disposition");
41+
String contentType = httpConn.getContentType();
42+
contentLength = httpConn.getContentLength();
43+
44+
if (disposition != null) {
45+
// extracts file name from header field
46+
int index = disposition.indexOf("filename=");
47+
if (index > 0) {
48+
fileName = disposition.substring(index + 10,
49+
disposition.length() - 1);
50+
}
51+
} else {
52+
// extracts file name from URL
53+
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
54+
fileURL.length());
55+
}
56+
57+
// output for debugging purpose only
58+
System.out.println("Content-Type = " + contentType);
59+
System.out.println("Content-Disposition = " + disposition);
60+
System.out.println("Content-Length = " + contentLength);
61+
System.out.println("fileName = " + fileName);
62+
63+
// opens input stream from the HTTP connection
64+
inputStream = httpConn.getInputStream();
65+
66+
} else {
67+
throw new IOException(
68+
"No file to download. Server replied HTTP code: "
69+
+ responseCode);
70+
}
71+
}
72+
73+
public void disconnect() throws IOException {
74+
inputStream.close();
75+
httpConn.disconnect();
76+
}
77+
78+
public String getFileName() {
79+
return this.fileName;
80+
}
81+
82+
public int getContentLength() {
83+
return this.contentLength;
84+
}
85+
86+
public InputStream getInputStream() {
87+
return this.inputStream;
88+
}
89+
}

0 commit comments

Comments
 (0)