[英]what is the file equivalent to ImageIO
Hi to you all java experts. 大家好,各位Java专家。
I have this piece of code I could finally put together that works: (it's mostly java with a little ADF code) 我有这段代码,我最终可以将其组合在一起:(主要是带有少量ADF代码的Java)
public String upload(){
UploadedFile myfile = this.getFile();
FacesContext fctx = FacesContext.getCurrentInstance();
ServletContext servletCtx =
(ServletContext)fctx.getExternalContext().getContext();
String imageDirPath = servletCtx.getRealPath("/");
String nomdefichier = myfile.getFilename();
String mimetype = nomdefichier.substring(nomdefichier.length() - 3);
try {
InputStream inputStream = myfile.getInputStream();
BufferedImage input = ImageIO.read(inputStream);
File outputFile =
new File( System.getProperty("user.home") + File.separator + this.path + File.separator + nomdefichier);
ImageIO.write(input, mimetype, outputFile);
} catch (Exception ex) {
// handle exception
}
FacesMessage message =
new FacesMessage(mimetype + "Successfully uploaded file " + nomdefichier +
" (" + myfile.getLength() + " bytes)" + mimetype);
fctx.addMessage(null, message);
return null;
}
This codes uploads a picture just fine. 此代码可以上传图片。 I would really like to know if there is a file equivalent to ImageIO.write so that I could upload PDF, DOCX and such.
我真的很想知道是否有一个等效于ImageIO.write的文件,以便我可以上载PDF,DOCX等文件。
Thanks in advance for any response. 预先感谢您的任何回复。
Best regards. 最好的祝福。
Marc Arbour 马克·阿伯
A simplified version of your code could be written as follows (omitting some of the JSF related stuff). 您的代码的简化版可以编写如下(省略一些与JSF相关的内容)。
InputStream in = myFile.getInputStream();
Files.copy(in, Paths.get(yourPath));
You can write a byte array or an InputStream
to a file with the java.nio.file.Files class (since Java 1.7). 您可以使用java.nio.file.Files类(从Java 1.7开始)将字节数组或
InputStream
写入文件。
// Saving data from an inputstream to a file:
Files.copy(inputStream, targetPath, copyOptions...);
// Saving a byte array to a file:
Files.write(path, byteArray, openOptions...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.