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;
}
{
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;
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;
}