2014-01-23 3 views
0

만들기 "/"때문에 다음 문제가 파일 이름 앞에, 나는 다음 오류 때문에 파일에 액세스하지 못할 잠시 :안드로이드 FileNotFoundException이 안드로이드를두고 어떤 이유로 파일

을 파일입니다 /data/data/tk.yteditors.london2013/files/

어떻게 해결할 수 있으며 File 클래스에서 내 파일 앞에 "/"를 추가하지 못하게합니까?

자세한 내용이 필요하면 질문하십시오.

01-23 16:59:15.174: W/System.err(26732): java.io.FileNotFoundException: /1043005842.html: open failed: ENOENT (No such file or directory) 
01-23 16:59:15.204: W/System.err(26732): at libcore.io.IoBridge.open(IoBridge.java:416) 
01-23 16:59:15.204: W/System.err(26732): at java.io.FileInputStream.<init>(FileInputStream.java:78) 
01-23 16:59:15.214: W/System.err(26732): at java.io.FileInputStream.<init>(FileInputStream.java:105) 
01-23 16:59:15.214: W/System.err(26732): at tk.yteditors.london2013.lib.ftp.SimpleFTP.stor(SimpleFTP.java:151) 
01-23 16:59:15.214: W/System.err(26732): at tk.yteditors.london2013.ConfirmActivity.sendFtp(ConfirmActivity.java:109) 
01-23 16:59:15.214: W/System.err(26732): at tk.yteditors.london2013.ConfirmActivity$1.run(ConfirmActivity.java:92) 
01-23 16:59:15.214: W/System.err(26732): at java.lang.Thread.run(Thread.java:856) 
01-23 16:59:15.224: W/System.err(26732): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) 
01-23 16:59:15.224: W/System.err(26732): at libcore.io.Posix.open(Native Method) 
01-23 16:59:15.224: W/System.err(26732): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 
01-23 16:59:15.224: W/System.err(26732): at libcore.io.IoBridge.open(IoBridge.java:400) 
01-23 16:59:15.224: W/System.err(26732): ... 6 more 

활성제 코드 :

public void sendFtp() throws Exception{ 
    SimpleFTP ftp = new SimpleFTP(); 
    ftp.connect("<SECRET_IP>", 21, "<SECRET_UN>", "<SECRET_PASS>"); 
    ftp.bin(); 
    ftp.stor(new File(getIntent().getExtras().getString("fileName"))); 
    ftp.disconnect(); 
} 

기타 코드 :

나는 그것을 전체 경로를 주면 내가, 그것은 단지 모든 경로 구분 기호를 제거하고 무엇 이건
package tk.yteditors.london2013.lib.ftp; 

import java.io.*; 
import java.net.*; 
import java.util.*; 


/** 
* SimpleFTP is a simple package that implements a Java FTP client. 
* With SimpleFTP, you can connect to an FTP server and upload multiple files. 
* <p> 
* Copyright Paul Mutton, 
*   <a href="http://www.jibble.org/">http://www.jibble.org/</a> 
* 
*/ 
public class SimpleFTP { 


/** 
* Create an instance of SimpleFTP. 
*/ 
public SimpleFTP() { 

} 


/** 
* Connects to the default port of an FTP server and logs in as 
* anonymous/anonymous. 
*/ 
public synchronized void connect(String host) throws IOException { 
    connect(host, 21); 
} 


/** 
* Connects to an FTP server and logs in as anonymous/anonymous. 
*/ 
public synchronized void connect(String host, int port) throws IOException { 
    connect(host, port, "anonymous", "anonymous"); 
} 


/** 
* Connects to an FTP server and logs in with the supplied username 
* and password. 
*/ 
public synchronized void connect(String host, int port, String user, String pass) throws IOException { 
    if (socket != null) { 
     throw new IOException("SimpleFTP is already connected. Disconnect first."); 
    } 
    socket = new Socket(host, port); 
    reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
    writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); 

    String response = readLine(); 
    if (response.startsWith("220")) { 

    } else{ 
     throw new IOException("SimpleFTP received an unknown response when connecting to the FTP server: " + response); 
    } 

    sendLine("USER " + user); 

    response = readLine(); 
    if (!response.startsWith("331") && !response.startsWith("220")) { 
     throw new IOException("SimpleFTP received an unknown response after sending the user: " + response); 
    } 

    sendLine("PASS " + pass); 

    response = readLine(); 
    if (!response.startsWith("230") && !response.startsWith("220")) { 
     throw new IOException("SimpleFTP was unable to log in with the supplied password: " + response); 
    } 

    // Now logged in. 
} 


/** 
* Disconnects from the FTP server. 
*/ 
public synchronized void disconnect() throws IOException { 
    try { 
     sendLine("QUIT"); 
    } 
    finally { 
     socket = null; 
    } 
} 


/** 
* Returns the working directory of the FTP server it is connected to. 
*/ 
public synchronized String pwd() throws IOException { 
    sendLine("PWD"); 
    String dir = null; 
    String response = readLine(); 
    if (response.startsWith("257")) { 
     int firstQuote = response.indexOf('\"'); 
     int secondQuote = response.indexOf('\"', firstQuote + 1); 
     if (secondQuote > 0) { 
      dir = response.substring(firstQuote + 1, secondQuote); 
     } 
    } 
    return dir; 
} 


/** 
* Changes the working directory (like cd). Returns true if successful. 
*/ 
public synchronized boolean cwd(String dir) throws IOException { 
    sendLine("CWD " + dir); 
    String response = readLine(); 
    return (response.startsWith("250")); 
} 


/** 
* Sends a file to be stored on the FTP server. 
* Returns true if the file transfer was successful. 
* The file is sent in passive mode to avoid NAT or firewall problems 
* at the client end. 
*/ 
public synchronized boolean stor(File file) throws IOException { 
    if (file.isDirectory()) { 
     throw new IOException("SimpleFTP cannot upload a directory."); 
    } 

    String filename = file.getName(); 

    return stor(new FileInputStream(file.getName()), filename); 
} 

/** 
* Sends a file to be stored on the FTP server. 
* Returns true if the file transfer was successful. 
* The file is sent in passive mode to avoid NAT or firewall problems 
* at the client end. 
*/ 
public synchronized boolean stor(InputStream inputStream, String filename) throws IOException { 

    BufferedInputStream input = new BufferedInputStream(inputStream); 

    sendLine("PASV"); 
    String response = readLine(); 
    if (!response.startsWith("227")) { 
     throw new IOException("SimpleFTP could not request passive mode: " + response); 
    } 

    String ip = null; 
    int port = -1; 
    int opening = response.indexOf('('); 
    int closing = response.indexOf(')', opening + 1); 
    if (closing > 0) { 
     String dataLink = response.substring(opening + 1, closing); 
     StringTokenizer tokenizer = new StringTokenizer(dataLink, ","); 
     try { 
      ip = tokenizer.nextToken() + "." + tokenizer.nextToken() + "." + tokenizer.nextToken() + "." + tokenizer.nextToken(); 
      port = Integer.parseInt(tokenizer.nextToken()) * 256 + Integer.parseInt(tokenizer.nextToken()); 
     } 
     catch (Exception e) { 
      throw new IOException("SimpleFTP received bad data link information: " + response); 
     } 
    } 

    sendLine("STOR " + filename); 

    Socket dataSocket = new Socket(ip, port); 

    response = readLine(); 
    if (!response.startsWith("150")) { 
     throw new IOException("SimpleFTP was not allowed to send the file: " + response); 
    } 

    BufferedOutputStream output = new BufferedOutputStream(dataSocket.getOutputStream()); 
    byte[] buffer = new byte[4096]; 
    int bytesRead = 0; 
    while ((bytesRead = input.read(buffer)) != -1) { 
     output.write(buffer, 0, bytesRead); 
    } 
    output.flush(); 
    output.close(); 
    input.close(); 

    response = readLine(); 
    return response.startsWith("226"); 
} 


/** 
* Enter binary mode for sending binary files. 
*/ 
public synchronized boolean bin() throws IOException { 
    sendLine("TYPE I"); 
    String response = readLine(); 
    return (response.startsWith("200")); 
} 


/** 
* Enter ASCII mode for sending text files. This is usually the default 
* mode. Make sure you use binary mode if you are sending images or 
* other binary data, as ASCII mode is likely to corrupt them. 
*/ 
public synchronized boolean ascii() throws IOException { 
    sendLine("TYPE A"); 
    String response = readLine(); 
    return (response.startsWith("200")); 
} 


/** 
* Sends a raw command to the FTP server. 
*/ 
private void sendLine(String line) throws IOException { 
    if (socket == null) { 
     throw new IOException("SimpleFTP is not connected."); 
    } 
    try { 
     writer.write(line + "\r\n"); 
     writer.flush(); 
     if (DEBUG) { 
      System.out.println("> " + line); 
     } 
    } 
    catch (IOException e) { 
     socket = null; 
     throw e; 
    } 
} 

private String readLine() throws IOException { 
    String line = reader.readLine(); 
    if (DEBUG) { 
     System.out.println("< " + line); 
    } 
    return line; 
} 

private Socket socket = null; 
private BufferedReader reader = null; 
private BufferedWriter writer = null; 

private static boolean DEBUG = false; 


} 

마지막 폴더를 보여줍니다 이름이, 앞에 슬래시가있는 그리고 만약 내가 그 파일을 받았다면, 슬래시가 튀어 나옵니다

추신 : 당신은 내가 좌절 한 것을 볼 수 있습니다 ...

+0

코드를 게시하십시오. – njzk2

+0

코드도 추가 –

답변

0

추가 된 '/'를 설명하기 위해 파일 위치 문자열을 편집 할 수 있어야합니다 ('../'을 사용해야 할 수도 있음).

코드를 보지 않고서는 이것이 작동하는지 확인할 수 없습니다.

EDIT : 코드를보고, 변수 filename을 편집하여 첫 번째 '/'문자를 삭제하면 추가 된 '/'문자를 수정할 수 있습니다.

+0

다시 확인할 수 있습니까? – rhbvkleef