File / FileInputStream问题

时间:2015-12-29 05:53:07

标签: java api methods jfilechooser

这段代码出现问题已经有了很长一段时间,并且已经在多个不同的场合和天使上观看并玩过它,并且似乎无法接受以红色下划线标记的文件(下面的代码)这属于Dropbox Api我试图创建的方法是将客户端文件上传到dropBox ...我使用JFileChooser打开文件然后将其放入选定的文件而不是fileInputStream这个问题出现的地方无论我尝试和解决它的方式,都不会出现问题。我以前从未使用过这个API,但是看过很多不同的exp。在查看代码后,代码和静止似乎无法获得它?

package daword;

 import java.util.ArrayList;
import java.awt.Desktop;
import java.io.File;
import com.dropbox.core.DbxAccountInfo;
import com.dropbox.core.*;
///import com.dropbox.core.DbxEntry.File;
import com.dropbox.core.json.JsonReader;
import com.dropbox.core.json.JsonReader.FileLoadException;
import java.awt.BorderLayout;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Locale;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.stage.FileChooser;
import javax.imageio.ImageIO;
import javax.naming.Context;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.DefaultEditorKit.CopyAction;
import javax.swing.text.DefaultEditorKit.CutAction;


public class dropBox extends JFrame implements ActionListener{
  File inputFile;   
private boolean logedIn = false;
JFileChooser fc;
 String dirName;
final String APP_KEY = "00000";
final String APP_SECRET = "00000";
private JButton connect, upload;
ImageIcon im1;
DbxClient client;
JLabel jlab;
JPanel jpan;
ImageIcon login;
BorderLayout borderlayout;
JTextField field = new JTextField(15);
JPopupMenu menu = new JPopupMenu("Menu");

//boolean  isAuthTokenReady = false;
InetSocketAddress add;
//boolean connection = false;
public JButton jb;



public dropBox(){
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(600,600);
setLocationRelativeTo(null);
setResizable(false);
setLookAndFeel();

JOptionPane.showMessageDialog(null,"1.Please Click Login\n2.Click Options\n3.Use How You Please!",
"Instructions", JOptionPane.WARNING_MESSAGE);
internet();

borderlayout = new BorderLayout();
getClipboard();
im1 = new ImageIcon(getClass().getResource("dropBox.gif"));
jlab= new JLabel(im1);
jlab.setLayout(null);
add(jlab);
login = new ImageIcon(getClass().getResource("login.gif"));
connect = new JButton( login);

connect.setSize(180, 57);
jlab.add(connect);
connect.addActionListener(this);


upload = new JButton ("upload");
upload.setBounds(0, 0, 220, 30);
upload.addActionListener(this);
jlab.add(upload);
BorderLayout bord = new BorderLayout();



setVisible(true);
}



 public void internet (){
Socket sock = new Socket();
  add = new InetSocketAddress("www.google.com", 80);
   try{
       wifi= true;
       sock.connect(add, 3000);
       System.out.println("Connection Present");
       ImageIcon img = new ImageIcon("/images/wifi.png");
       jb = new JButton(img);


   } catch(IOException e){

       System.out.println(" No Connection");
   }

 }





@Override
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
Object uploadS = evt.getSource();
if(source == connect){
try{
drop();
    }catch(Exception e){
JOptionPane.showMessageDialog(null,"Unable To Connect :( Please Try Again ..",
"ALERT", JOptionPane.WARNING_MESSAGE);

}

}else if(uploadS == upload){

       if (logedIn = true){
           try {
               uploadFile();
           } catch (DbxException ex) {
               Logger.getLogger(dropBox.class.getName()).log(Level.SEVERE, null, ex);
           } catch (IOException ex) {
               Logger.getLogger(dropBox.class.getName()).log(Level.SEVERE, null, ex);
           }
       }



}
}



private void setLookAndFeel() {
try{
UIManager.setLookAndFeel("UIManager.getSystemLookAndFeelClassName");
}catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
UnsupportedLookAndFeelException e){
}
}


public static String getClipboard() {
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try {
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return (String) t.getTransferData(DataFlavor.stringFlavor);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


// JButton Login
public void drop() throws IOException, DbxException{

 String userLocale = Locale.getDefault().toString();
DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);
DbxRequestConfig config = new DbxRequestConfig("Daword",
Locale.getDefault().toString());
DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(config, appInfo);
// Have the user sign in and authorize your app.
String authorizeUrl = webAuth.start();
System.out.println("You Will Be Redirected To: " + authorizeUrl);
JOptionPane.showMessageDialog(null,"Go To:"+ authorizeUrl,
"" +
"", JOptionPane.WARNING_MESSAGE);
java.awt.Desktop.getDesktop().browse(java.net.URI.create(authorizeUrl));
System.out.println("Enter Your auth code in this prompt and hit enter and wait..");



String userA = JOptionPane.showInputDialog(null, "Enter your auth code here:"); //Prompt for

if (userA.isEmpty()) {
System.exit(1);
}
System.out.println(userA.trim()); //For testing purposes print the trimmed auth code to console.
// [Omitted Code] //
// This will fail if the user enters an invalid authorization code.


DbxAuthFinish authFinish = webAuth.finish(userA);
String accessToken = authFinish.accessToken;
 client = new DbxClient(config, accessToken);
DbxAuthInfo authInfo = new DbxAuthInfo(authFinish.accessToken, appInfo.host);
System.out.println("Linked account: " + client.getAccountInfo().displayName);


JOptionPane.showMessageDialog(null, "Welcome"+
client.getAccountInfo().displayName+ authFinish.accessToken+
" And Welcome To Our Community!");

 }




public void uploadFile() throws FileNotFoundException, DbxException, IOException{

 fc = new JFileChooser();
 fc.setMultiSelectionEnabled(true);
 fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

int dialog = fc.showSaveDialog(this);

 if (dialog == JFileChooser.APPROVE_OPTION) {
  inputFile = fc.getSelectedFile().getAbsoluteFile();




    try (FileInputStream inputStream = new FileInputStream(inputFile)) {

        DbxEntry.File uploadedFile = client.uploadFile("",
        DbxWriteMode.add(), inputFile.length(), inputStream);
        String sharedUrl = client.createShareableUrl("");
        System.out.println("Uploaded: " + uploadedFile.toString() + " URL " + sharedUrl);
    }      



}
}
}

2 个答案:

答案 0 :(得分:0)

听起来你错过了import java.io.File;

答案 1 :(得分:0)

这已基本上解决了发生的事情,因为smarx指出不足以让我注意导入java.io,因为我有一个相互冲突的dbx文件导入..我也没注意到我没有包含存储我的访问代码的代码所以每次运行我应该登录的程序或者应该存储密钥。简而言之,文件路径也引起了问题,因为我假设文件选择器得到了代码,并将它传递给它没有做的输入流,文件位置exp的绝对路径:user / me / desktop / cool.txt是需要。我查看了Dropbox文档,详细了解了正确的参数以及帮助操作此社区将它拼凑在一起,感谢大家的回复。