java 文件上传

1.ajax部分:

  function FileUpload()
    {
    var fileinput = $('#image');
        $.ajaxFileUpload
        (
            {
                url:'<s:url action="Staff11!uploadImage"/>',
                secureuri:false,
                fileElementId:'image',
                dataType: 'json',
                beforeSend:function()
                {               
                },
                success: function (response, status)
                {
                    if(response.status=="success")
                    {
                        $('#img1').attr('src', response.imageurl);
                        $("input[name='imageURL']").attr("value",response.imagename);
                        //$('#oldsrc').val(response.imageurl);
                        document.getElementById("cstaff_oldsrc").value=response.imageurl;
                        document.getElementById("cstaff_oldImgUrl").value=response.imagename;
                    }
                    else
                    {
                        alert(response.errormsg);
                    }
                },
                error: function (data, status, e)
                {
                    alert(e);
                }
            }
        )
        return false;

    }


2.action部分:

 public void uploadImage() throws Exception {
CommonFunction.writeInforLogStart(logger, this.getClass().getName(), "uploadImage");
Ajaximage ai = new Ajaximage();
if (image == null || !image.isFile()) {
ai.setStatus("fail");
ai.setErrormsg("正しいイメージを選択してください");
} else {
String newImageName = null;
long now = new Date().getTime();
String path = Config.Instance().getValue("UploadimagePhisicaldir");
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
String index = "cultureStaff.jpg";
if (index != null) {
newImageName = now + index;
} else {
newImageName = Long.toString(now);
}
ai.setStatus("success");
webpath = Config.Instance().getValue("Uploadimagewebpath");
ai.setImageurl(webpath + newImageName);
ai.setImagename("/" + newImageName);

BufferedOutputStream bos = null;
BufferedInputStream bis = null;

FileInputStream fis = new FileInputStream(image);
bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(new File(dir,
newImageName));
bos = new BufferedOutputStream(fos);
byte[] buf = new byte[4096];
int len = -1;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
bos.flush();
bos.close();
bis.close();
}
ActionContext ctx = ActionContext.getContext();
HttpServletResponse response = (HttpServletResponse) ctx
.get(StrutsStatics.HTTP_RESPONSE);
response.setContentType("textml");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
response.setHeader("Pragma", "no-cache"); // HTTP 1.0
response.setDateHeader("Expires", 0);

// String json = CommonFunction.getJsonResult(page.getList(),
// page.getTotalCount());
PrintWriter out = response.getWriter();
String json = JsonUtil.object2json(ai);
out.write(json);
CommonFunction.writeInforLogEnd(logger, this.getClass().getName(), "uploadImage");
return;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值