1

我是一个新手用户,试图弄清楚如何使用 uuencode 方法。我们有一个只允许上传单个文本文件的表单。现在看起来只会上传 zip 文件。我正在尝试包含 uuencode 方法来将字节转换为字符串,这样我们就不必修改其余代码来适应二进制文件。

原始代码:

public void SettingUpload(File inputfile) { 
    this.inputfile = inputfile;
}

我把它改成

public void SettingUpload(File inputfile){
UUEncoder uuec = new UUEncoder();
    try{
        InputStream is = new FileInputStream(inputfile);
        OutputStream os = new FileOutputStream(inputfile);
        uuec.encodeBuffer(is, os);
        this.inputfile = inputfile;
    }catch (Throwable error) {
        reportError(error, "Error converting zipfile");
    }

}

当我测试它时,我得到了一个 java.io.EOFException。我抓住了 uuencoded 文件并手动对其进行了 uudecoded。当我试图解压它时,

bash1:~>unzip s6b0c9e663c74f72941bd8271a5fac3b.bin 
 Archive:  s6b0c9e663c74f72941bd8271a5fac3b.bin

 End-of-central-directory signature not found.  Either this file is not
 a zipfile, or it constitutes one disk of a multi-part archive.  In the

编辑:

我将其更改为:

 public void SettingUpload(File inputfile){
    UUEncoder uuec = new UUEncoder();
        try{
            InputStream is = new FileInputStream(inputfile);
                   File OutputFile=new File("Output");
                        OutputFile.createNewFile();
            OutputStream os = new FileOutputStream(OutputFile);
            uuec.encodeBuffer(is, os);
            this.OutputFile = OutputFile;
        }catch (Throwable error) {
            reportError(error, "Error converting zipfile");
        }

 }

我收到以下错误:

cannot find symbol
symbol  : variable OutputFile
4

1 回答 1

5

正如浩准所说,你不应该这样做:

InputStream is = new FileInputStream(inputfile); // Good
OutputStream os = new FileOutputStream(inputfile); // Bad

您应该输出到单独的文件,否则第一次写入时,您将丢弃原始文件。

更新示例

这对我来说很好......

public static void main(String[] args) {

    File inputfile = new File("file/to/be/encoded");
    File outFile = new File("out.uue");

    UUEncoder uuec = new UUEncoder();
    InputStream is = null;
    OutputStream os = null;
    try {

        is = new FileInputStream(inputfile);
        os = new FileOutputStream(outFile);
        uuec.encodeBuffer(is, os);

    } catch (Exception error) {
        error.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (Exception e) {
        }
        try {
            os.close();
        } catch (Exception e) {
        }
    }

    File newFile = new File("decoded.jpg");
    UUDecoder decoder = new UUDecoder();
    try {

        is = new FileInputStream(outFile);
        os = new FileOutputStream(newFile);
        decoder.decodeBuffer(is, os);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (Exception e) {
        }
        try {
            os.close();
        } catch (Exception e) {
        }
    }

}

另外,我会从您的编码方法返回输出文件

public void SettingUpload(File inputfile) throws IOException {
    UUEncoder uuec = new UUEncoder();
    File outFile = File.createTempFile("encoded", "uue");
    InputStream is = null;
    OutputStream os = null;
    try{
        is = new FileInputStream(inputfile);
        os = new FileOutputStream(outFile );
        uuec.encodeBuffer(is, os);
    } finally {
        try {
            is.close();
        } catch (Exception e) {
        }
            try {
        os.close();
        } catch (Exception e) {
        }
    }
    return outFile;
}

你永远不应该压制异常。呼叫者如何知道是否出现问题?

此外,如果您打开一个流,您有责任关闭它,因此请确保您关闭了您的流。

于 2012-10-04T06:32:30.933 回答