(1)页面:
<s:decorate id="coverField" template="../layout/edit.xhtml" >
<ui:define name="label">Upload Watermark</ui:define>
<s:fileUpload id="cover" data="#{waterMarkManager.localPath}" onkeypress="return false;" accept="images/*" contentType="image/*" fileSize="10000000" fileName="#{waterMarkManager.currentFileName}"
size="23">
</s:fileUpload>
</s:decorate>
(2)类中:
private byte[] localPath;
public byte[] getLocalPath() {
return localPath;
}
public void setLocalPath(byte[] localPath) {
this.localPath = localPath;
}
(3)FTPTools.java类:
public File getFileFromBytes(byte[] b, String outputFile){
BufferedOutputStream stream = null;
File file = null;
try{
file = new File(outputFile);
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (Exception e){
e.printStackTrace();
} finally{
if (stream != null){
try{
stream.close();
} catch (IOException e1){
e1.printStackTrace();
}
}
}
return file;
}
这样上传的文件或者图片就会保存到本地的硬盘中了,就是利用上面那个方法就可以搞定。
本文介绍了一种使用特定组件实现文件上传的方法,并展示了如何将上传的文件或图片保存到本地硬盘的过程。通过定义文件上传组件并设置相关属性,如文件大小限制、文件类型等,可以轻松实现文件上传功能。此外,还提供了一个Java方法,用于将字节数组转换为文件。
1万+

被折叠的 条评论
为什么被折叠?



