struts2.0 文件上传问题

liujun_1028 2010-01-22 11:51:43
我的代码

public String execute() throws Exception
{
for (int i = 0; i < file.size(); ++i)
{
InputStream is = new FileInputStream(file.get(i));

String root = ServletActionContext.getRequest().getRealPath("/upload");

File destFile = new File(root, this.getFileFileName().get(i));

OutputStream os = new FileOutputStream(destFile);

byte[] buffer = new byte[400];

int length = 0;

while ((length = is.read(buffer)) > 0)
{
os.write(buffer, 0, length);
}

is.close();

os.close();
}

return SUCCESS;

}
问题是 我提交上传后 并没有 在 “upload”文件夹下出现我上传的文件?谁能帮我解惑?(控制台未出现任何异常)
...全文
26 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
redlotus_lyn 2010-01-22
  • 打赏
  • 举报
回复
1.引用commons-io.x.x.jar

2.java代码:

String path = request.getRealPath("/upload");
File saveFile = new File(path, fileFileName);
FileUtils.copyFile(file, saveFile);
sotom 2010-01-22
  • 打赏
  • 举报
回复
if (file.getSize() != 0) {
InputStream stream = null;
stream = file.getInputStream();
OutputStream bos = null;
bos = new FileOutputStream(outPutPath);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
stream.close();
}


这个是我的Struts2 文件上传代码,你自己对照下试试。

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧