UploadForm上传一个form中的多个file

苦苦的潜行者 2011-09-14 04:16:22
先上图


在一个form里面实现两个文件的不同时上传,两个上传互不干涉.

之前只有一个file 和submit,即Select Excel File那一行;现在我加上了下面的Feedback File 一行file和submit.
结果导致只能Feedback这个submit能够上传文件,Select那一行的submit一上传就出错,说是The input file was not found.
然后我后台输出Select一行的file.getFileName()为空.这是为什么呢!

上代码

这是form体
<html:form action="uploadAction.do?method=upFile"
enctype="multipart/form-data">
<c:choose>
<c:when test="${zn == 'pilot'}">
<input type="radio" name="name" value="pilot"
checked="checked" onclick="getZoneDate('pilot');"/>
<font color="red">PRFQ1</font>
</c:when>
<c:otherwise>
<input type="radio" name="name" value="pilot" onclick="getZoneDate('pilot');"/>
<font color="red">PRFQ1</font>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${zn == 'week'}">
<input type="radio" name="name" value="week"
checked="checked" onclick="getZoneDate('week');"/>
<font color="red">PRFQ2</font>
</c:when>
<c:otherwise>
<input type="radio" name="name" value="week" onclick="getZoneDate('week');"/>
<font color="red">PRFQ2</font>
</c:otherwise>
</c:choose>
<br/>
Select Excel File:<html:file property="theFile" />  
<html:submit value="upload"/>  
<br/>
FeedBack    File:<html:file property="theFile" />  
<html:submit value="upload" onclick="this.form.action='uploadAction.do?method=upFBFile'"/>  
</html:form>


这是后台action过程

Select一行的submit
public ActionForward upFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String filePathName = "";
if (form instanceof UploadForm) {
String encoding = request.getCharacterEncoding();

if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
response.setContentType("text/html; charset=gb2312");
}
UploadForm theForm = (UploadForm) form;
FormFile file = theForm.getTheFile();
String contentType = file.getContentType();
String zoneName = theForm.getName();
String size = (file.getFileSize() + " bytes");
String fileName = file.getFileName();
//
System.out.println("fileName="+fileName); //这里打印为"fileName=",明显没有值
String timeStr = df.getTime("yyyyMMddhhmmss");
try {
InputStream stream = file.getInputStream();
String filePath = request.getRealPath("/");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = null;
filePathName = filePath+ "files" + "\\"+ timeStr+file.getFileName();
String pathName = request.getSession().getServletContext().getRealPath("/") + "files";
File files= new File(pathName);
if(files.exists()&&files.isDirectory()){
bos = new FileOutputStream(filePath + "files"+ "\\"
+ timeStr+file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
}else{
boolean flag = files.mkdir();
if(flag){
bos = new FileOutputStream(filePath + "files"+ "\\"
+ timeStr+file.getFileName());
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();
} catch (Exception e) {
}
request.setAttribute("contentType", contentType);
request.setAttribute("size", size);
request.setAttribute("fileName", fileName);
request.setAttribute("filePathName", filePathName);
request.setAttribute("zoneName", zoneName);
return mapping.findForward("display");
}
return null;
}


Feedback一行的submit
public ActionForward upFBFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String filePathName = "";
if (form instanceof UploadForm) {
String encoding = request.getCharacterEncoding();

if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
response.setContentType("text/html; charset=gb2312");
}
UploadForm theForm = (UploadForm) form;
FormFile file = theForm.getTheFile();
String contentType = file.getContentType();
String zoneName = theForm.getName();
String size = (file.getFileSize() + " bytes");
String fileName = file.getFileName();
String timeStr = df.getTime("yyyyMMddhhmmss");
try {
InputStream stream = file.getInputStream();
String filePath = request.getRealPath("/");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = null;
filePathName = filePath+ "files" + "\\"+ timeStr+file.getFileName();
String pathName = request.getSession().getServletContext().getRealPath("/") + "files";
File files= new File(pathName);
if(files.exists()&&files.isDirectory()){
bos = new FileOutputStream(filePath + "files"+ "\\"
+ timeStr+file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
}else{
boolean flag = files.mkdir();
if(flag){
bos = new FileOutputStream(filePath + "files"+ "\\"
+ timeStr+file.getFileName());
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();
} catch (Exception e) {
}
request.setAttribute("contentType", contentType);
request.setAttribute("size", size);
request.setAttribute("fileName", fileName);
request.setAttribute("filePathName", filePathName);
request.setAttribute("zoneName", zoneName);
return mapping.findForward("displayFB");
}
return null;
}


这俩代码几乎似乎一样的,为什么会导致这样呢!
...全文
353 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
苦苦的潜行者 2011-09-21
  • 打赏
  • 举报
回复
结贴:
4楼问题尚未解决,最近在看struts及struts标签,希望能从中获益,暂时共用一个file,希望以后问题解决再来分享答案.
苦苦的潜行者 2011-09-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 softroad 的回复:]
Select Excel File:<html:file property="theFile" />
FeedBack File:<html:file property="theFile" />

楼主这么有2个submit
[/Quote]

应该怎么修改呢?
苦苦的潜行者 2011-09-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 softroad 的回复:]
property="theFile" 两个名字 一样了。
[/Quote]

我现在突然发现一种现象
就是我Select行的file没用!!
事实上这两个submit共用第二行的file
......
好惨!怎么办!
如果实在没有好的办法我只有共用file了
安心逍遥 2011-09-14
  • 打赏
  • 举报
回复
页面上要设置一下

好像也不能用request获得

具体的忘了。楼主搜一下吧

祝你好运
softroad 2011-09-14
  • 打赏
  • 举报
回复
Select Excel File:<html:file property="theFile" />
FeedBack File:<html:file property="theFile" />

楼主这么有2个submit
softroad 2011-09-14
  • 打赏
  • 举报
回复
property="theFile" 两个名字 一样了。

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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