Last active
February 3, 2017 06:28
-
-
Save codingmiao/9990c8c30e9f96bc04e61fbcd684774a to your computer and use it in GitHub Desktop.
向七牛云上传图片的swing程序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.wowtools.img.qiniuimg; | |
import com.qiniu.common.QiniuException; | |
import com.qiniu.http.Response; | |
import com.qiniu.storage.UploadManager; | |
import com.qiniu.util.Auth; | |
import org.json.JSONObject; | |
import org.wowtools.common.utils.ResourcesReader; | |
import javax.swing.*; | |
import javax.swing.filechooser.FileNameExtensionFilter; | |
import java.awt.*; | |
import java.awt.event.FocusAdapter; | |
import java.awt.event.FocusEvent; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import java.io.File; | |
import java.net.URI; | |
import java.util.UUID; | |
/** | |
* @author liuyu | |
* @date 2017/2/3 | |
*/ | |
public class MainUi { | |
private JPanel MainUi; | |
private JButton 上传Button; | |
private JTextPane textPaneLog; | |
private JTextField textFieldfilePath; | |
private JButton 选择Button; | |
private JTextField textFieldSFileName; | |
private JButton 生成Button; | |
private JTextField textFieldSUrl; | |
private JButton 浏览Button; | |
private UiBean uiBean; | |
public MainUi() { | |
String strJson = ResourcesReader.readStr(MainUi.getClass(), "qiniu.json"); | |
JSONObject jo = new JSONObject(strJson); | |
uiBean = new UiBean(jo.getString("ACCESS_KEY"), jo.getString("SECRET_KEY"), jo.getString("bucketname"), jo.getString("baseUrl")); | |
生成Button.addMouseListener(new MouseAdapter() { | |
@Override | |
public void mouseClicked(MouseEvent e) { | |
super.mouseClicked(e); | |
setSFileName(UUID.randomUUID().toString()); | |
} | |
}); | |
选择Button.addMouseListener(new MouseAdapter() { | |
@Override | |
public void mouseClicked(MouseEvent e) { | |
super.mouseClicked(e); | |
JFileChooser chooser = new JFileChooser(); | |
chooser.setApproveButtonText("确定"); | |
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); | |
FileNameExtensionFilter filter = new FileNameExtensionFilter( | |
"jpg、png、gif", "jpg", "gif", "png"); | |
chooser.addChoosableFileFilter(filter); | |
chooser.showDialog(new JLabel(), "选择"); | |
File file = chooser.getSelectedFile(); | |
setFilePath(file.getPath()); | |
} | |
}); | |
上传Button.addMouseListener(new MouseAdapter() { | |
@Override | |
public void mouseClicked(MouseEvent evt) { | |
super.mouseClicked(evt); | |
Auth auth = Auth.create(uiBean.getACCESS_KEY(), uiBean.getSECRET_KEY()); | |
// 创建上传对象 | |
UploadManager uploadManager = new UploadManager(); | |
try { | |
// 调用put方法上传 | |
Response res = uploadManager.put(uiBean.getFilePath(), uiBean.getsFileName(), auth.uploadToken(uiBean.getBucketname())); | |
// 打印返回的信息 | |
appendLog(res.bodyString()); | |
String url = uiBean.getBaseUrl() + uiBean.getsFileName(); | |
textFieldSUrl.setText(url); | |
} catch (QiniuException e) { | |
Response r = e.response; | |
// 请求失败时打印的异常的信息 | |
System.out.println(r.toString()); | |
try { | |
// 响应的文本信息 | |
appendLog(r.bodyString()); | |
} catch (QiniuException e1) { | |
appendLog(e1.getMessage()); | |
} | |
} catch (Exception e) { | |
appendLog(e.getMessage()); | |
} | |
} | |
}); | |
textFieldSFileName.addFocusListener(new FocusAdapter() { | |
@Override | |
public void focusLost(FocusEvent e) { | |
setSFileName(textFieldSFileName.getText()); | |
} | |
}); | |
浏览Button.addMouseListener(new MouseAdapter() { | |
@Override | |
public void mouseClicked(MouseEvent e) { | |
super.mouseClicked(e); | |
String url = uiBean.getBaseUrl() + uiBean.getsFileName(); | |
textFieldSUrl.setText(url); | |
Desktop desktop = Desktop.getDesktop(); | |
try { | |
desktop.browse(new URI(url)); | |
} catch (Exception e1) { | |
appendLog(e1.getMessage()); | |
} | |
} | |
}); | |
} | |
public static void main(String[] args) { | |
MainUi mainUi = new MainUi(); | |
JFrame frame = new JFrame("上传图片"); | |
frame.setContentPane(mainUi.MainUi); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.pack(); | |
frame.setVisible(true); | |
mainUi.setSFileName(mainUi.uiBean.getsFileName()); | |
frame.setSize(767, 250); | |
frame.setLocation(300, 300); | |
} | |
private void setSFileName(String sFileName) { | |
uiBean.setsFileName(sFileName); | |
textFieldSFileName.setText(uiBean.getsFileName()); | |
} | |
private void setFilePath(String filePath) { | |
uiBean.setFilePath(filePath); | |
textFieldfilePath.setText(filePath); | |
} | |
private void appendLog(String msg) { | |
String log = uiBean.getLog(); | |
log += "\n" + msg; | |
uiBean.setLog(log); | |
textPaneLog.setText(log); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependencies> | |
<dependency> | |
<groupId>com.qiniu</groupId> | |
<artifactId>qiniu-java-sdk</artifactId> | |
<version>[7.0.0, 7.1.99]</version> | |
</dependency> | |
<dependency> | |
<groupId>org.wowtools</groupId> | |
<artifactId>catframe-common</artifactId> | |
<version>1.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.json</groupId> | |
<artifactId>json</artifactId> | |
<version>20160212</version> | |
</dependency> | |
</dependencies> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"ACCESS_KEY": "KEY", | |
"SECRET_KEY": "KEY", | |
"bucketname": "wowtoolsorg", | |
"baseUrl": "http://ooxx.clouddn.com/" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.wowtools.img.qiniuimg; | |
import com.qiniu.util.Auth; | |
import java.util.UUID; | |
/** | |
* ui数据绑定bean | |
* | |
* @author liuyu | |
* @date 2017/2/3 | |
*/ | |
class UiBean { | |
// 设置好账号的ACCESS_KEY和SECRET_KEY | |
private String ACCESS_KEY; | |
private String SECRET_KEY; | |
// 要上传的空间 | |
private String bucketname; | |
private String log = ""; | |
// 上传到七牛后保存的文件名 | |
private String sFileName = UUID.randomUUID().toString();// http://7xlvcv.com1.z0.glb.clouddn.com/my-java.png | |
// 上传文件的路径 | |
private String filePath = ""; | |
//默认域名 | |
private String baseUrl; | |
public UiBean(String ACCESS_KEY, String SECRET_KEY, String bucketname, String baseUrl) { | |
this.ACCESS_KEY = ACCESS_KEY; | |
this.SECRET_KEY = SECRET_KEY; | |
this.bucketname = bucketname; | |
this.baseUrl = baseUrl; | |
} | |
public String getLog() { | |
return log; | |
} | |
public String getsFileName() { | |
return sFileName; | |
} | |
public void setsFileName(String sFileName) { | |
this.sFileName = sFileName; | |
} | |
public String getFilePath() { | |
return filePath; | |
} | |
public void setFilePath(String filePath) { | |
this.filePath = filePath; | |
} | |
public void setLog(String log) { | |
this.log = log; | |
} | |
public String getACCESS_KEY() { | |
return ACCESS_KEY; | |
} | |
public String getSECRET_KEY() { | |
return SECRET_KEY; | |
} | |
public String getBucketname() { | |
return bucketname; | |
} | |
public String getBaseUrl() { | |
return baseUrl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment