当前位置: 代码迷 >> J2EE >> struts Action多线程怎么返回数据给前台
  详细解决方案

struts Action多线程怎么返回数据给前台

热度:66   发布时间:2016-04-17 23:00:35.0
struts Action多线程如何返回数据给前台
    public String picUpload(){
     HttpServletResponse response=ServletActionContext.getResponse();
        SimpleDateFormat sDateFormat;
        Random r=new Random(); 
        realpath=ServletActionContext.getServletContext().getRealPath("/"+laiyuang);
        realpath=realpath+"/file/";  
        realpath=realpath.replace("\\", "/");  
if(fileupload!=null){
File savedir=new File(realpath);
if  (!savedir.isFile()){
new File(realpath).mkdir();
}
}
    
     for(i=0;i<fileupload.length;i++){    
     new Thread(new MultipleUploadAction(fileupload[i],fileuploadContentType[i],fileuploadFileName[i],laiyuang,i,)
     realpath,savePath400,savePath1024,response)).start();
     }
     return SUCCESS;
    }

@Override
public void run() {
String extName=null;
if(fileuploadFileNamea.lastIndexOf(".")>=0){
extName=fileuploadFileNamea.substring(fileuploadFileNamea.lastIndexOf("."));
extName=extName.substring(1,extName.length());
}
String saveFile=realpath+nowTimeStr+rannum+i+"."+extName;
saveFile=saveFile.replace("\\", "/");

try{
FileUtils.copyFile(fileuploada, new File(saveFile));
}catch(Exception e){
e.printStackTrace();
}

PrintWriter out=null;
System.out.println(response);
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
try{
out=response.getWriter();
out.print(saveFile);
out.flush();
}catch(IOException e){
e.printStackTrace();
}finally{
out.close();
}

}
加上红色的就出问题了。

前台:
<script type="text/javascript">
function uploadImage(obj) {
$(document)
.ready(
function() {             
alert("ee");
var options = {    
url : "tool/picUpload.action",
type : "POST",
dataType : "script",  
success : processJson,
error: eJson  
};
$("#touxinagform").ajaxSubmit(options);
return false;
});
}

function processJson(msg){
alert(msg);

                        
        }
        

function eJson(){
alert("ttaa");
}

------解决思路----------------------
response.setContentType("text/html;charset=UTF-8");
------解决思路----------------------
加同步试试呗   不过我怀疑不是这里的问题  你用的是struts2吧   struts2是线程安全的    有报错信息吗?
------解决思路----------------------
out.flush(); 和 out.close(); 去掉试试   输出流会自动刷新和关闭的
  相关解决方案