使用FileItemStream在Java中处理二进制文件
我有一个允许上传二进制文件的Web应用程序。我必须解析它们并将内容1:1保存到字符串中,然后保存到数据库中 当我在unix机器上使用uuencode对二进制文件进行编码时,它就可以工作了。有没有一种方法可以在java中自动实现这一点使用FileItemStream在Java中处理二进制文件,java,file-upload,upload,binary,encode,Java,File Upload,Upload,Binary,Encode,我有一个允许上传二进制文件的Web应用程序。我必须解析它们并将内容1:1保存到字符串中,然后保存到数据库中 当我在unix机器上使用uuencode对二进制文件进行编码时,它就可以工作了。有没有一种方法可以在java中自动实现这一点 if (isMultipart) { //Create a new file upload handler ServletFileUpload upload = new ServletFileUpload();
if (isMultipart) {
//Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
//Parse the request
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (!item.isFormField()) {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line;
licenseString = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
// Generate License File
licenseString += line + "\n";
}
}
}
session.setAttribute("licenseFile", licenseString);
System.out.println("adding licensestring to session. ");
}
它当然适用于所有上传的非二进制文件。如何扩展它以支持二进制文件?更好的方法是将上载写入临时文件,然后从那里处理它:
if (!item.isFormField()) {
InputStream stream = new BufferedInputStream(item.getInputStream());
BufferedOutputStream output = null;
try {
output = new BufferedOutputStream(new FileOutputStream(your_temp_file, false));
int data = -1;
while ((data = input.read()) != -1) {
output.write(data);
}
} finally {
input.close();
output.close();
}
}
现在您有了一个临时文件,它与上载的文件相同,您可以从那里进行“其他”计算。更好的方法是将上载写入临时文件,然后从那里处理它:
if (!item.isFormField()) {
InputStream stream = new BufferedInputStream(item.getInputStream());
BufferedOutputStream output = null;
try {
output = new BufferedOutputStream(new FileOutputStream(your_temp_file, false));
int data = -1;
while ((data = input.read()) != -1) {
output.write(data);
}
} finally {
input.close();
output.close();
}
}
现在您有了一个临时文件,它与上载的文件相同,您可以从那里进行“其他”计算。您可以使用commons_fileupload lib(在此处选中:) 文件在这里:
您的案例在官方网站上得到了很好的解释。您可以使用commons_fileupload lib(请在此处查看:)
// save to file
// =======================================
InputStream is = new BufferedInputStream(item.openStream());
BufferedOutputStream output = null;
try {
output = new BufferedOutputStream(new FileOutputStream("temp.txt", false));
int data = -1;
while ((data = is.read()) != -1) {
output.write(data);
}
} finally {
is.close();
output.close();
}
// read content of file
// =======================================
System.out.println("content of file:");
try {
FileInputStream fstream = new FileInputStream("temp.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
licenseString = "";
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println(javax.xml.bind.DatatypeConverter.printBase64Binary(strLine.getBytes()));
licenseString += javax.xml.bind.DatatypeConverter.printBase64Binary(strLine.getBytes()) + "\n";
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
文件在这里:
你的案例在官方网站上解释得很好。谢谢,这是一个很好的输入,但我已经试过了。它不能解决我的“编码”问题:(谢谢,这是一个很好的输入,但我已经尝试过了。它不能解决我的“编码”问题:(
// save to file
// =======================================
InputStream is = new BufferedInputStream(item.openStream());
BufferedOutputStream output = null;
try {
output = new BufferedOutputStream(new FileOutputStream("temp.txt", false));
int data = -1;
while ((data = is.read()) != -1) {
output.write(data);
}
} finally {
is.close();
output.close();
}
// read content of file
// =======================================
System.out.println("content of file:");
try {
FileInputStream fstream = new FileInputStream("temp.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
licenseString = "";
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println(javax.xml.bind.DatatypeConverter.printBase64Binary(strLine.getBytes()));
licenseString += javax.xml.bind.DatatypeConverter.printBase64Binary(strLine.getBytes()) + "\n";
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}