use*_*051 1 java zip file-upload uuencode
我是一个新手用户试图弄清楚如何使用uuencode方法.我们有一个表单,只允许上传一个文本文件.现在看起来只会上传zip文件.我试图包含uuencode方法将字节转换为String,这样我们就不必修改其余的代码来容纳二进制文件.
原始代码:
public void SettingUpload(File inputfile) {
this.inputfile = inputfile;
}
Run Code Online (Sandbox Code Playgroud)
我改成了
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");
}
}
Run Code Online (Sandbox Code Playgroud)
当我测试它时,我得到了一个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
Run Code Online (Sandbox Code Playgroud)
编辑:
我改成了:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
cannot find symbol
symbol : variable OutputFile
Run Code Online (Sandbox Code Playgroud)
正如蒿准评论,你不应该这样做:
InputStream is = new FileInputStream(inputfile); // Good
OutputStream os = new FileOutputStream(inputfile); // Bad
Run Code Online (Sandbox Code Playgroud)
您应该输出到单独的文件,否则第一次写入时,您将丢弃原始文件.
用例子更新
这对我来说很好......
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) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
另外,我会从你的编码方法返回输出文件
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;
}
Run Code Online (Sandbox Code Playgroud)
你永远不应该压制异常.如果出现问题,来电者将如何知道?
此外,如果您打开一个流,则需要关闭它,因此请确保关闭流.
| 归档时间: |
|
| 查看次数: |
2550 次 |
| 最近记录: |