Bootstrap Image Upload instance,
Imported package:
import java.io.File;import java.util.Iterator;import java.util.UUID;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.FileItemFactory;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.gzzs.util.service.HandlerPath;import com.opensymphony.xwork2.ActionSupport;
Html:
<Div style = "margin-top: 10px; margin-left: 180px "> <input id =" specialrecommendfile "type =" file "size =" 30 "name =" file "> <button type =" button "onclick =" UploadSpecialRecommendPic () "class =" btn-warning btn-lg "> upload </button> <input type = hidden class =" span5 "id =" specialRecommendPic "name =" riIndexPic "> </div>
Js:
function UploadSpecialRecommendPic() {$.ajaxFileUpload({url : '/GZZSServer/s_util/uploadSpecialRecommendPic.action',secureuri : false,fileElementId : 'specialrecommendfile',dataType : 'json',success : function(d, status) {var data = eval('(' + d + ')');alert(data.msg);if (data.flag == 1) {$("#SpecialTopicPicShow").attr("src", data.path);$("#specialRecommendPic").val(data.picName);}},error : function(data, status, e) {alert(e);}});}
Action:
Public String uploadSpecialRecommendPic () {try {String path; path = "C: //"; FileUtils. copyFile (file, new File (path); result = "{flag: \" 1 \ ", msg: \" operation successful \ ", path: \" "+ HandlerPath. getHostSimulateAuthorLogoPath () + fileFileName + "\", picName: \ "" + fileFileName + "\"} "; return SUCCESS;} catch (Exception e) {e. printStackTrace (); result = "{flag: \" 0 \ ", msg: \" operation failed \ "}"; return SUCCESS ;}}
Instance image: