gpt4 book ai didi

java - java中的uuencode zipfile

转载 作者:行者123 更新时间:2023-11-29 08:03:50 30 4
gpt4 key购买 nike

我是一名新手,正在尝试了解如何使用 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

最佳答案

正如浩准评论的那样,你不应该这样做:

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;
}

你永远不应该抑制异常。调用者如何知道是否出了问题?

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

关于java - java中的uuencode zipfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12721472/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com