import java.awt.List;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.omg.DynamicAny.NameValuePair;
public class Upload {
public static void main (String[] args) {
System.out.println(Imgur("C:\\Users\\username\\Desktop\\image.jpg", "clientID"));
}
public static String Imgur (String imageDir, String clientID) {
//create needed strings
String address = "https://api.imgur.com/3/image";
//Create HTTPClient and post
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(address);
//create base64 image
BufferedImage …Run Code Online (Sandbox Code Playgroud) 我只是想知道是否有一种简单的方法可以将一个小文件上传到ftp服务器.我已经检查了Apache Commons Net库,但说实话这似乎很复杂.有没有更简单的方法将小文件上传到ftp?
结束使用Apache Commons Net Library,并不是太难.
所以我有一个类,它的作用是读取文件的内容,然后根据内容做一些事情.
package maple;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JOptionPane;
public class SetSettings {
public SetSettings() {
BufferedReader reader;
File f = new File (Shared.saveLocation + "Settings.txt");
String line = "";
if (f.exists()) {
while (line != null) {
try {
//System.out.println("COMES HERE");
reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
line = reader.readLine();
System.out.println(line);
} catch (IOException e2) {
// TODO Auto-generated catch block
//e2.printStackTrace();
//JOptionPane.showMessageDialog(null, "Error Code: Bx001", "Error", 0);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
出于测试目的,我让它打印出.txt的每一行.而不是打印出.txt文件中的内容,它将打印出第一行,然后打开.无论.txt文件中有一行还是100行,它都会不断读取一行.如何让它读到下一个并在没有更多时停止?
所以有这行代码
String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encodeBase64String(baos.toByteArray()).toString(), "UTF-8");
data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(YOUR API KEY GOES HERE, "UTF-8");
Run Code Online (Sandbox Code Playgroud)
当我注册Imgur API时,我得到了一个client_id和一个client_secret,并想知道我在哪里使用哪个"你的API密钥就在这里"也在第二行的第一部分,它说"关键"是什么我进入那里?也是上传它的网站http://imgur.com/api/upload因为我看过几个不同的网站.
所以我想要的是程序运行时(它是系统托盘),这些小通知问题之一显示在屏幕右下方。我试过了。
trayIcon = new TrayIcon(image, "Title", popup);
trayIcon.setImageAutoSize(true);
trayIcon.displayMessage("Title", "MESSAGE HERE", TrayIcon.MessageType.ERROR) //THIS IS THE LINE THAT SHOULD SHOW THE MESSAGE
Run Code Online (Sandbox Code Playgroud)
程序运行时应该在哪里运行,并且参数正确的方法正确吗?
在我的项目中,我使用系统托盘,当我编译程序时,一切正常,我用于系统托盘的图标显示出来.
图标放在项目文件夹中,与图标相关的代码是
Image icon = Toolkit.getDefaultToolkit().getImage("Icon.png");
trayIcon = new TrayIcon(icon, "Program name", popup);
trayIcon.setImageAutoSize(true);
tray.add(trayIcon);
Run Code Online (Sandbox Code Playgroud)
正如我所说的,一切正常,但当我将项目导出为可运行的jar时,程序将运行,但图标不会显示,但它只是空白.
所以我有这条线
String line = null;
Run Code Online (Sandbox Code Playgroud)
在那之后,我有一个if语句,它告诉我错误是什么
令牌";"上的语法错误,{此标记后的预期
所以它说而不是";" 应该有一个"{".
该程序还告诉我,我需要一个额外的"}",这很可能是因为第一个问题而添加的.
解决方案:我必须将代码放在构造函数中.
if (f.exists()) {
try {
reader = new BufferedReader(new FileReader(
System.getProperty("user.home") + "/Desktop/Settings.txt"));
line = reader.readLine();
} catch (IOException e2) {
// ... etc....
Run Code Online (Sandbox Code Playgroud)