from实现struts上传,允许回调

很多时候,form表单提交,是通过浏览器去执行方法,一般情况下,我们不会去考虑需要回调。但是,我们使用from实现文件struts上传,然后,我们还需要它返回一个结果。这样,就可以看的得到我上传是否成功,且还可以根据上传是否成功去实现不同的功能:

以下是我模仿某个网站做的一些实现

实现这个功能还需要一个插件,就是jquery.form.js,源码地址https://malsup.github.io/jquery.form.js


代码:前台代码

<div id="addFubuGoods_photo" style="width:600px; height:470px;" class="easyui-dialog" title="添加景点图片信息"  data-options="iconCls:'icon-add',resizable:true,modal:true,closed:true" >
	<form method="post" enctype="multipart/form-data" id="upload_form" action="../upload/uploadFile_upload" >
		<label>景点图片:</label><input type="file" name="file" value="选择图片" id="viewPic" multiple="multiple" οnchange="previewMultipleImage (this,'view_show_pic',80,80)"  />
		<div id="view_show_pic" style="width:550px;height:300px;border:1px solid #ef6819;margin:20px;margin-bottom: 0px;"></div><br />
		<p style="margin:0px;margin-bottom: 10px;margin-left:20px;"><span style="color:red;">*</span><label style="color:#ccc;">我们将选取第一张相片作为你们产品的宣传主题画</label></p>
	</form>
	<input type="button" οnclick="upload_file()" name="upload" value="start_upload" style=" border-radius:20px; background-color: #00e3e3;border:none;padding:5px;margin-bottom: 10px;margin-left:30%;cursor: pointer;" />
	<input type="button" οnclick="cencel_upload()" name="cencel" value="cencel_upload" style=" border-radius:20px; background-color: #00e3e3;border:none;padding:5px;margin-bottom: 10px;margin-left:1%;cursor: pointer;" />
</div>
后台实现,就是使用sturts2的上传:

UploadFile.class

package com.qw.actions;

import java.io.File;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.qw.json.entities.JsonObject;
import com.qw.utils.UploadFile_Image;
import com.opensymphony.xwork2.ActionSupport;

public class UploadFile extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	private List<File> file;  //要上传的表单元素的name属性值             
	private List<String> fileContentType;  //上传文件的类型  
	private List<String> fileFileName;  //上传文件的名称
	private String savePath;  //用来接收依赖注入的文件保存路径
	private UploadFile_Image uploadImage=new UploadFile_Image();
	private JsonObject jsonObject;
	
	public String upload(){
		try {
			jsonObject.setObj(uploadImage.upload(file, fileContentType, fileFileName, getSavePath()));
		} catch (Exception e) {
			jsonObject.setObj("photo");
		}
		return "success";
	}

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public List<String> getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	public List<String> getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public JsonObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JsonObject jsonObject) {
		this.jsonObject = jsonObject;
	}
}
<pre name="code" class="java">UploadFile_Image.class

 
package com.qw.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class UploadFile_Image {
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmssSSS");
	private String houzui="";//后缀
	private String photo_path="";//图片路径
	private String countName="";//当前所有路径
	
	
	public String upload(List<File> file,List<String> fileContentType,List<String> fileFileName,String savePathMethod){
		FileOutputStream fos=null;
		FileInputStream fis=null;
		try {
			for(int i=0;i<file.size();i++){
				houzui=fileFileName.get(i).substring(fileFileName.get(i).lastIndexOf("."));
				countName=("LV"+sdf.format(new Date()))+houzui;
				photo_path+="pics/"+countName+",";
				fos=new FileOutputStream(savePathMethod+"\\"+countName);
				fis=new FileInputStream(file.get(i));
				byte[] bt=new byte[1024];
				int len=0;
				while((len=fis.read(bt))>0){
					fos.write(bt,0,len);
				}
				fos.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(fos!=null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fis!=null){
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return photo_path.substring(0,photo_path.lastIndexOf(","));
	}
}
js部分:

//上传图片
	function upload_file(){
		$("#upload_form").ajaxSubmit({
			type:'post',
			url:'../upload/uploadFile_upload',
			dataType:"json",
			success:function(data){
				if(data.obj!="photo"){
					$.post("../json/businessAction_updateGoodsInfo",{"testPro.viewId":viewId,"testPro.viewPic":data.obj},function(data){
						if(parseInt($.trim(data.result))==1){
							$.messager.show({
								title:'成功提示',
								msg:'景点图片上传成功...',
								timeout:2000,
								showType:'slide'
							});
							$("input[type=reset]").trigger("click");
							$("#business_fabuview_data").datagrid("reload");
						}else{
							$.messager.show({
								title:'失败提示',
								msg:'网络出小差了...',
								timeout:2000,
								showType:'slide'
							});
						}
					});
				}else{
					$.messager.show({
						title:'失败提示',
						msg:'网络出小差了...',
						timeout:2000,
						showType:'slide'
					});
				}
			}
		});
	}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值