无法在安卓中保存文件



美好的一天。

我需要将 PKCS10 CSR 保存在外部存储卡中。

但是,以下代码显示错误

java.io.FileNotFoundException:/storage/sdcard0pkcs10.req: open failed: EROFS (Read-only File system)

虽然 AndoidManifest.xml 正确包含

我觉得问题是目录路径,因为它显示 0 而不是/

package exam.blowfishcipher;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.security.*;
import org.spongycastle.jce.provider.BouncyCastleProvider;
import java.security.SecureRandom;
import javax.security.auth.x500.X500Principal;
import org.spongycastle.jce.PKCS10CertificationRequest;
import org.spongycastle.openssl.PEMWriter;
import android.os.Environment;
import android.util.*;
import android.widget.*;
public class PKCS10Generater
{
    static {
        Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider());
    }
    public static PKCS10CertificationRequest generateRequest(
            KeyPair pair)
            throws Exception
            {           
             return new PKCS10CertificationRequest(
                     "SHA256withRSA",
                     new X500Principal("CN=Test CA Certificate"),
                     //new X500Principal("CN=end"),
                     pair.getPublic(),
                     null,
                     pair.getPrivate());
            }
    public static void pemEncodeToFile(String filename, Object obj, char[] password) throws Exception{
        PEMWriter pw = new PEMWriter(new FileWriter(filename));
           Log.e("Position", "PEMWriter");
           if (password != null && password.length > 0) {
               pw.writeObject(obj, "DESEDE", password, new SecureRandom());
           } else {
               pw.writeObject(obj);
           }
           pw.flush();
           pw.close();
    }
    public static void reqGen() throws Exception
    {
        //create the keys
        Log.e("Position", "reqGen");
        KeyPair pair = Utils.generateRSAKeyPair();
        //modified 20130203
        PKCS10req pkcs10req = new PKCS10req();
        PKCS10CertificationRequest request = pkcs10req.generateRequest(pair);
        pemEncodeToFile(Environment.getExternalStorageDirectory()+"pkcs10.req", request, null);
        Log.e("Position", "getExternalStorage");
        PEMWriter pemWrt = new PEMWriter( new OutputStreamWriter(System.out));
        pemWrt.writeObject(request);
        pemWrt.close();
    }
}

只需修改行

pemEncodeToFile(Environment.getExternalStorageDirectory()+"pkcs10.req", request, null);

pemEncodeToFile(Environment.getExternalStorageDirectory()+"/pkcs10.req", request, null);

相关内容

  • 没有找到相关文章

最新更新