代码Struts部分package com.ssh_rooms.action;import java.io.*;
import java.net.URLDecoder;
import java.util.*;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;@Controller
public class MyUploadAction extends Action 
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception 
{
String rPath=request.getParameter("pPath");
String picPath=URLDecoder.decode(rPath,"utf-8");
HttpSession session=request.getSession();
File carrier=new File(picPath);
long fmaxSize=carrier.length();
long limitSize=1024*1024;
String fname=carrier.getName().toUpperCase();
List<String> fendName=new ArrayList<String>();
fendName.add("JPG");
fendName.add("JPEG");
fendName.add("BMP");
fendName.add("GIF");
fendName.add("PNG");
String targetPath="D:\\JAVA\\tools\\Tomcat 6.0\\webapps\\ssh_rooms\\img_tenement\\"+fname;
File targetFile=new File(targetPath);
if(fmaxSize>limitSize)
{
String pic_msg="<img src='"+request.getContextPath()+"/ico/error.png' />"
+"文件过大,请选择其他图片!";
System.out.println(pic_msg);
session.setAttribute("pic_msg", pic_msg); return mapping.findForward("myUpload");
}
else if(fmaxSize<limitSize||fmaxSize==limitSize)
{
int count=0;
for(String name:fendName)
{
if(fname.endsWith(name))
{
String pic_msg="<img src='"+request.getContextPath()+"/ico/pass.png' />"
+"文件上传成功!";
System.out.println(pic_msg);
FileInputStream fis=new FileInputStream(carrier);
FileOutputStream fos=new FileOutputStream(targetFile);
byte[] bytes=new byte[1204*1024];
int length=0;
while((length=fis.read(bytes))!=-1)
{
fos.write(bytes,0,length); 
}
fis.close();
fos.close();
session.setAttribute("pic_msg", pic_msg); }
else if(!fname.endsWith(name))
{
count++;
if(count==fendName.size())
{
String pic_msg="<img src="+request.getContextPath()+"/ico/error.png />"
+"文件格式不正确,请选择图片!";
System.out.println(pic_msg);
session.setAttribute("pic_msg", pic_msg); }
}
}
return mapping.findForward("myUpload");
}
else if(picPath==null)
{
return mapping.findForward("myUpload");
}
else
return mapping.findForward("myUpload");
}
}<!-- ======================分割线 ====================== -->jsp部分<div class="pic_msg" id="pic_msg">
<div class="pic_msg_title">
</div>
<div class="pic_msg_content" align="center"><br>
${pic_msg}
</div>
</div>现在的问题是
System.out.println(pic_msg);显示的信息是准确的
但是
${pic_msg}总是显示第一次满足条件的信息
以后其他条件得到的结果都不显示,仅仅显示第一次满足条件的信息

解决方案 »

  1.   

    看的乱乱的。
    每次存放的session中的时候,你先进行remove然后再放!
      

  2.   

    应该是lz上传的图片的大于了上传图片的最大大小,所有只能满足第一个要求,所有只输出一个值,还有建议lz先删除以前的,然后再保存到session中。
      

  3.   

    session.removeAttribute("pic_msg");
    session.setAttribute("pic_msg", pic_msg); 
    这样之后还是不行,仍然出现同样的问题
    还有Jquery前台提示信息
    每次必须先刷新一次页面
    ${pic_msg}才会显示
    但是后台已经输出了
      

  4.   

    这里说满足第一次条件的信息
    指的这三个判断条件,谁先满足,谁就先显示
    以后再满足其他条件,后台能准确打印出相应的结果
    但是将这个结果作为提示信息存放到session转发给前台显示的时候
    就总是显示第一次出现的结果
    我后台尝试过
    session.removeAttribute("pic_msg");这个没有效果
    前台也试过
    <%session.removeAttribute("pic_msg"); %>
    这个有效果:页面装载完毕,这个属性也就清掉了,div那里总是空的