jsp中使用xhEditor上传文件

39 篇文章 0 订阅
public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        BufferedInputStream fileIn = new 
        BufferedInputStream(request.getInputStream()); 
        String fn = request.getParameter("filename");
        
        byte[] buf = new byte[1024];
        File file = new File("d:/" + fn); 
          
        BufferedOutputStream fileOut = new BufferedOutputStream(new 
        FileOutputStream(file)); 
          
        while (true) { 
            // 读取数据
            int bytesIn = fileIn.read(buf, 0, 1024); 
            System.out.println(bytesIn); 
            if (bytesIn == -1) 
            { 
                break; 
            } 
            else 
            { 
                fileOut.write(buf, 0, bytesIn); 
            } 
        } 
          
        fileOut.flush(); 
        fileOut.close(); 
        System.out.println(file.getAbsolutePath());
        //uploadFile(request);
        response.getWriter().println("{\"err\":\"\",\"msg\":\"" + file.getAbsolutePath() + "\"}");
        return null;
    }

 

我在spring中添加上面的方法;

 

 

在xheditoer源码作了小小改动,添加一个参数把文件名传到服务器去:

function postFile(fromfile,toUrl,callback,onProgress)
		{
			xhr = new XMLHttpRequest(),upload=xhr.upload;
			xhr.onreadystatechange=function(){if(xhr.readyState===4)callback(xhr.responseText);};
			if(upload)upload.οnprοgress=function(ev){onProgress(ev.loaded);};
			else onProgress(-1);//不支持进度
			if(toUrl.indexOf("&") == -1 && (toUrl.indexOf("?") == -1)){
				toUrl += "?filename="+fromfile.fileName
			}else{
				toUrl += "&filename="+fromfile.fileName;
			}


			xhr.open("POST", toUrl);
			xhr.setRequestHeader('Content-Type', 'application/octet-stream');
			xhr.setRequestHeader('Content-Disposition', 'attachment; name="'+inputname+'"; filename="'+fromfile.fileName+'"');
			if(xhr.sendAsBinary)xhr.sendAsBinary(fromfile.getAsBinary());
			else xhr.send(fromfile);
		}

 

这个就可以FF中使用html5特用的Ajax上传文件传文件了

 

原来一直认为只能表单提交方式上传文件,现在该改一下了,哈哈

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值