我在Java Web项目上工作。我有一个xthml页面,它有一个rich:fileUpload。在监听器中,我得到输入流如下:
<xsl:element name="ChildAges">
<xsl:for-each select="//Age">
<xsl:choose>
<xsl:when test=". < 2">
<Age>2</Age>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:element>
然后按钮保存我试图通过以下代码将文件上传到磁盘:
public void uploadListener(FileUploadEvent event) {
try {
in = event.getUploadedFile().getInputStream();
fileName = event.getUploadedFile().getName();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(in);
}
}
我收到以下错误:
public void uploadFile(){
try {
String path=some_path + currentReport.getFilename();
File targetFile = new File(path);
java.nio.file.Files.copy(
in,
targetFile.toPath(),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
有什么建议吗?提前致谢和问候。
答案 0 :(得分:0)
添加检查输入流是否已关闭然后调用reset
试试这个:
public void uploadFile(){
try {
String path=some_path + currentReport.getFilename();
File targetFile = new File(path);
java.nio.file.Files.copy(
in,
targetFile.toPath(),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
in.reset();
java.nio.file.Files.copy(
in,
targetFile.toPath(),
StandardCopyOption.REPLACE_EXISTING);
}
}
答案 1 :(得分:0)
好的,所以我自己解决了这个问题。我在doUpload函数中关闭了inputStream,如下所示:
public void uploadListener(FileUploadEvent event) {
try {
in = event.getUploadedFile().getInputStream();
fileName = event.getUploadedFile().getName();
} catch (IOException e) {
e.printStackTrace();
}
}
public void uploadFile(){
try {
String path=some_path + currentReport.getFilename();
File targetFile = new File(path);
java.nio.file.Files.copy(
in,
targetFile.toPath(),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
IOUtils.closeQuietly(in);
}
}