jsp提交表单时假如没有选择上传文件,action中就会报空指针异常,选择了上传文件就不会,代码如下,求指导

J2EE 码拜 8年前 (2016-03-16) 1289次浏览
jsp简化代码
<script type=”text/javascript”>
function imgChange(next) {
if(next != null)
document.getElementById(next).style.display = “”;
}
</script>
<form   action=”evaluatefinish.action?d=<%=pid %>&o=<%=oid%>” method=”post” enctype=”multipart/form-data” >
<table class=”user-info_tb” style=”margin:15px 0;” id=”table1″>
<textarea name=”assess” id=”assess1″ placeholder=”字数不超过30字”></textarea>
<input type=”file” name=”upload” id=”photoFile0″ size=”50″ onChange=”return imgChange(“photoFile1″)” />
<input type=”file” name=”upload” id=”photoFile1″ onChange=”return imgChange(“photoFile2″)” size=”50″ style=”display: none;”/>
<input type=”file” name=”upload” id=”photoFile2″ onChange=”return imgChange(“photoFile3″)” size=”50″ style=”display: none;”/>
<input type=”file” name=”upload” id=”photoFile3″ onChange=”return imgChange(null)” size=”50″ style=”display: none;”/>
<input type=”submit” value=”发表” />
</table>
</form>
action 简化代码
private static final int BUFFER_SIZE=16 * 1024;
private String title;//文件标题
private File[]upload;//上传文件或对象
private String[] uploadFileName;//上传文件
private String uploadContentType;//上传文件类型
private String savePath;//保存文件的目录路径
String assess;
都设置了get/set方法,在这里省略。
public String EvaluateFinish() throws IOException{
if(this.getUploadFileName().length>0){
System.out.println(“bb”);
String read=ServletActionContext.getServletContext().getRealPath(this.getSavePath());
File file = new File(read);
System.out.println(read);
file.mkdirs();
for(int i=0;i<this.upload.length;i++)
{
String dstPath=read+”\”+this.uploadFileName[i];
File dstFile=new File(dstPath);
copy(this.upload[i],dstFile);
System.out.println(“lujin:”+dstPath);
}
}
}
xml配置简化如下
<action name=”evaluatefinish” class=”action.Action_OrdersInfo” method=”EvaluateFinish”>
<param name=”savePath”>/image/pid</param>
<result name=”success”>evaluate_orders.jsp</result>
</action>
假如填写了评价内容和选择了上传文件,功能正常,但是只填写评价内容,没有选择上传文件时,就会报错,代码如下
页面报错是500
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
action.Action_OrdersInfo.EvaluateFinish(Action_OrdersInfo.java:277)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:870)
ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293)
ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
麻烦高手指导下,有点急!
解决方案

2

判断一下 要是文件里面有东西i在 length  要不然  就是空 一个null  是不可能有 length的

2

页面做一个hidden类型的input,给file加个onchange事件,假如不为空的话给hidden一个value。上传的时候根据这个hidden标签判断是不是有选择文件,有就保存文件,没有就跳过。

2

你在拿到流之后获取里面文件时给你获取的这个动作做一个trycatch假如抛了空指针异常那么就给客户端返回一个信息说是空的,或直接finally跳过去

2

你先判断是不是空在取length

2

this.getUploadFileName() 这个为null 。

2

你要先判断下能否为空啊…..

28

引用:
Quote: 引用:
Quote: 引用:

你在拿到流之后获取里面文件时给你获取的这个动作做一个trycatch假如抛了空指针异常那么就给客户端返回一个信息说是空的,或直接finally跳过去

这个方法理论上是可以的
还有个方法
jsp中加一个隐藏标签,假如选择了文件,就把隐藏标签赋值。提交之后后台判断假如隐藏标签有值就上传,假如没值就直接跳过获取文件和上传的代码

谢谢啊,本人朋友之前是用的你这种方法实现的。

那你直接用就行了 有什么问题吗


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明jsp提交表单时假如没有选择上传文件,action中就会报空指针异常,选择了上传文件就不会,代码如下,求指导
喜欢 (0)
[1034331897@qq.com]
分享 (0)