struts2上传图片的全过程 自己版
function toAdd() { if(befoSubmit()) { $.ajax({ cache : true, type : "POST", url : "<%=basePath%>check/addCheckWineInfoOK", data : $("#addForm").serialize(), error : function(request) { $("#errorMsg").show(); }, success : function(data) { if(data) { $("#okMsg").show(); setTimeout(function(){$("#okMsg").hide();}, 4000); } else { $("errorMsg").show(); } } }); } } <s:form id="addForm" action="check/addCheckWineInfoOK" method="post" enctype="multipart/form-data" theme="simple" > <fieldset> <div style="float: left;"> <table> <tr> <td><b><label>产品批次号</label></b></td> <td><s:select list="#{‘C123‘:‘C123‘,‘C456‘:‘C456‘,‘C789‘:‘C789‘}" name="checkWine.productId" cssClass="large-input" /></td> <td></td> </tr> <tr> <td><b><label>葡萄酒检测时间</label></b></td> <td><input name="dateStr" type="text" readonly="readonly" value="单击此处选择时间" id="rangeA" onclick="changeDate(this.value);"/></td> <td></td> </tr> <tr> <td><b><label>检测人</label></b></td> <td><input class="text-input large-input datepicker" type="text" id="large-input" name="checkWine.checkPerson" /></td> <td></td> </tr> <tr> <td><b><label>感官</label></b></td> <td><input class="text-input large-input datepicker" type="text" id="large-input" name="checkWine.feel" /></td> <td></td> </tr> <tr> <td><b><label>净含量(ml)</label></b></td> <td><input class="text-input large-input datepicker" type="text" id="large-input" name="checkWine.content" /></td> <td></td> </tr> <tr> <td><b><label>酒精度(%vol)</label></b></td> <td><input class="text-input large-input datepicker" type="text" id="large-input" name="checkWine.alc" /></td> <td></td> </tr> <tr> <td><b><label>总糖(g/l)</label></b></td> <td><input class="text-input large-input datepicker" type="text" id="large-input" name="checkWine.total_sugar" /></td> <td></td> </tr> <tr> <td><b><label>挥发酸(g/l)</label></b></td> <td><input class="text-input large-input datepicker" type="text" id="large-input" name="checkWine.acidDegree" /></td> <td></td> </tr> <tr> <td><b><label>总SO2(mg/l)</label></b></td> <td><input class="text-input large-input datepicker" type="text" id="large-input" name="checkWine.totalSO2" /></td> <td></td> </tr> <tr> <td><b><label>干浸物(g/l)</label></b></td> <td><input class="text-input large-input datepicker" type="text" id="large-input" name="checkWine.solidDegree" /></td> <td></td> </tr> <tr> <td><b><label>备注</label></b></td> <td><input class="text-input large-input datepicker" type="text" id="large-input" name="checkWine.other" /></td> <td></td> </tr> <tr> <td style="text-align: right; width: 120px;font-size: 15px;font-weight: bold;">上传质检报告:</td> <td id="more"> <s:file name="file"></s:file> <input type="button" value="继续添加" onclick="addMore();"> </td> </tr> <tr> <td><input class="button" type="submit" value="确认添加"/></td> </tr> </table> </div> </fieldset> <div class="clear"></div> </s:form>
如上 jsp ;
配置文件如下:
<action name="addCheckWineInfoOK" class="addCheckWineInfoAction"> <interceptor-ref name="fileUpload"> <!-- 设置文件上传大小 --> <param name="maximumSize">4096000</param> <!-- 设置文件上传类型 --> <param name="allowedTypes"> application/vnd.ms-powerpoint </param> </interceptor-ref> <!-- 自定义了拦截器后必手动定义默认的拦截器,否则默认的拦截器不会被执行 --> <interceptor-ref name="defaultStack"></interceptor-ref> <!-- <result name="success" type="json"> <param name="root">result</param> </result> --> <result name="success" type="redirectAction"> toSelectAllCheckWineInfo </result> </action>
package com.jsjxy.wine.action.check.wine; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.text.ParseException; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import com.jsjxy.wine.action.BaseAction; import com.jsjxy.wine.bean.pojo.CheckWine; import com.jsjxy.wine.service.dao.base.DAOException; import com.jsjxy.wine.utils.CreateDateUtil; @Controller @Scope("prototype") @Transactional public class AddCheckWineInfoAction extends BaseAction { private String dateStr;// 用于接收页面提交过来的时间字符串 private List<File> file; private List<String> fileFileName; private List<String> fileContentType; private List<String> dataUrl; private CheckWine checkWine; @Override public String execute() { try { System.out.println("file------"+file); HttpServletRequest request= ServletActionContext.getRequest(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/"; if (file != null) { dataUrl = new ArrayList<String>(); String imgpath = "upload\\"; for (int i = 0; i < file.size(); ++i) { InputStream is = new FileInputStream(file.get(i)); String path = ServletActionContext.getServletContext().getRealPath("/"); System.out.println("path ----------------------"+path); String fileName = Long.toString(System.currentTimeMillis()) + this.getFileFileName().get(i); File destFile = new File(path+imgpath, fileName); System.out.println("数据库url------"+basePath+imgpath+fileName); dataUrl.add(basePath+imgpath+fileName); OutputStream os = new FileOutputStream(destFile); byte[] buffer = new byte[400]; int length = 0; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } is.close(); os.close(); } String finalUrl = ""; for (String Iurl : dataUrl) { finalUrl = finalUrl + Iurl+";"; } checkWine.setImageUrl(finalUrl); } checkWine.setCheckWineTime(CreateDateUtil.getTimeStart(dateStr)); checkWineDAO.addCheckWineInfo(checkWine); result = true; } catch (DAOException e) { e.printStackTrace(); result = false; } catch (ParseException e) { e.printStackTrace(); result = false; }catch (Exception e) { e.printStackTrace(); } return super.execute(); } public String getDateStr() { return dateStr; } public void setDateStr(String dateStr) { this.dateStr = dateStr; } public List<File> getFile() { return file; } public void setFile(List<File> file) { this.file = file; } public List<String> getFileFileName() { return fileFileName; } public void setFileFileName(List<String> fileFileName) { this.fileFileName = fileFileName; } public List<String> getFileContentType() { return fileContentType; } public void setFileContentType(List<String> fileContentType) { this.fileContentType = fileContentType; } public List<String> getDataUrl() { return dataUrl; } public void setDataUrl(List<String> dataUrl) { this.dataUrl = dataUrl; } public CheckWine getCheckWine() { return checkWine; } public void setCheckWine(CheckWine checkWine) { this.checkWine = checkWine; } }
如上是上传过程
<s:iterator value="urls" id="imageUrl"> <tr><td><img src="${imageUrl}" width="800px;" height="500px;"> </td></tr> </s:iterator>
如上是图片显示过程
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。