java 异步上传_异步上传图片

web.xml

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID" version="2.5">

01_图片异步上传

index.html

index.htm

index.jsp

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

struts.xml

/p>

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

CustomerAction.java

package com.itkawa.upload;

import java.io.File;

import java.io.IOException;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;

import org.apache.struts2.ServletActionContext;

import org.itkawa.crm.utils.UploadUtils;

import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction extends ActionSupport {

private static final long serialVersionUID = -3281746108664320233L;

// 图片上传

private File image_upload;// 上传后的文件

private String image_uploadContentType;// 类型

private String image_uploadFileName;// 上传的文件名称

// 读取配置中的properties

private String uploadDir ="F:/crm/images";

public File getImage_upload() {

return this.image_upload;

}

public void setImage_upload(File image_upload) {

this.image_upload = image_upload;

}

public String getImage_uploadContentType() {

return this.image_uploadContentType;

}

public void setImage_uploadContentType(String image_uploadContentType) {

this.image_uploadContentType = image_uploadContentType;

}

public String getImage_uploadFileName() {

return this.image_uploadFileName;

}

public void setImage_uploadFileName(String image_uploadFileName) {

this.image_uploadFileName = image_uploadFileName;

}

public String do_saveImage() {

System.out.println("contentType : " + image_uploadContentType);

System.out.println("fileName : " + image_uploadFileName);

try {

String stuffix = image_uploadFileName.substring(image_uploadFileName.lastIndexOf("."));

// 文件名称命名

String path = UploadUtils.getPath(image_upload).concat(stuffix);

File file = new File(uploadDir + path);

if (!file.getParentFile().exists()) {

file.getParentFile().mkdirs();

}

FileUtils.copyFile(image_upload, file);

HttpServletResponse response = ServletActionContext.getResponse();

response.setContentType("text/html;charset=utf-8");

response.getWriter().write("{'path':'"+path+"'}"); // 返回json , 图片路径信息

} catch (IOException e) {

return NONE;

}

return NONE;

}

}

index.jsp

pageEncoding="UTF-8"%>

Insert title here

$(function() {

/*============== 异步上传文件 start ========================*/

$('#btn_submit').on('click', function() {

var fileName = $("#upload").val();

if (fileName == "") {

console.log("=== 没有选择文件 ====")

return;

}

//alert("fileName = "+fileName);

$.ajaxFileUpload({

url : '${pageContext.request.contextPath }/customer/saveImage',

secureuri : false,

fileElementId : 'image_upload',//file标签的id

type : 'post',

dataType : 'json', //返回值类型 一般设置为json

success : function(data, status) //服务器成功响应处理函数

{

console.log(data);

var src = "${pageContext.request.contextPath }/images"+data.path;

$("#img").attr("src",src);

},

error : function(data, status, e)//服务器响应失败处理函数

{

alert("服务器挂了....需要重启...");

}

});

});

/*============== 异步上传文件 end ========================*/

});

图片~~~~

在tomcat配置文件里添加虚拟路径

Context docBase="F:/crm/images" path="/upload/images"/>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值