struts2 多个文件上传 , jquery 的用法
??
jsp 页面?
<table id="table2" width="100%" border="1" cellspacing="0"cellpadding="0"><tr width="10%">操作</th><th nowrap="nowrap" align="left"><input type="button" value="删 除" id="deleteFile1"disabled="disabled" onclick="deleteFile()"style="text-align: center;"> <input type="button" value="添 加" id="addFile1"disabled="disabled" onclick="addFile()"style="text-align: center;"></td></tr><s:iterator value="pxjgfjVOList" id="result" status="status"><tr><td><input type="checkbox" name="choiceFile"value="<s:property value='cy_lsh'/>"></td><td align="center"><s:property value='cy_wjmc' /></td></tr></s:iterator><tr id="fileOne"><td><input type="checkbox" name="choiceFile"></td><td align="center"><s:file name="file" theme="simple" label="选择上传的文件"cssStyle="width:70%" /><font color="red">*</font></td></tr></table>
?
javascript :?
var $tr3;$(document).ready(function() {$tr2 = $("#fileOne").remove();});function addFile() {$tr2.clone().appendTo("#table2");}//删除选中的Filefunction deleteFile() {var choiceFiles = "";$('input[name="choiceFile"]').each(function() {if ($(this).attr("checked")) {if (this.value != "") {choiceFiles += this.value + ",";}$(this.parentNode.parentNode).remove(); // 删除当前节点的父节点的父节点}});$.ajax( {url : "file!deleteFile.action?lshs=" + choiceFiles,cache : false,success : function() {}});}function toSaveFile() {document.forms[1].action = "<%=path%>/cypxjg/sl!toSaveFile.action";document.forms[1].submit();}
??
struts.xml配置文件:??
<package name="file" extends="struts-default,json-default"namespace="/file"><action name="file"><result name="gzd">/file.jsp</result><result name="deleteOK" type="json"><param name="root">messageType</param></result><result name="getPO" type="json"><param name="root">filePO</param></result><result name="fileDown" type="stream"><param name="contentType">application/octet-stream</param><param name="contentDisposition">attachment;filename="${downloadFileName}" </param><param name="inputName">inputStream</param> <param name="bufferSize">4096</param> </result></action>
??
Action
import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class FileAction extends TaskGuideBaseAction {private static final long serialVersionUID = 1L;private List<File> file = new ArrayList<File>();private List<String> fileFileName;private String fileName;private List<String> fileContentType;/** * saveFile * @throws XException */public String toSaveFile() throws XException {User user = WebRBACRequestContext.getRBACRequestContext(ServletActionContext.getRequest()).getRequestUser();TransactionManager.beginTransaction();try {// add 上传文件for (int i = 0; i < file.size(); i++) {PxjgfjPO pxjgfjPO = new PxjgfjPO();pxjgfjPO.setCy_wjmc(fileFileName.get(i));pxjgfjPO.setCy_wjlx(fileContentType.get(i));InputStream in = new BufferedInputStream(new FileInputStream(file.get(i)), BUFFER_SIZE);pxjgfjPO.setCy_wjnr(in);bo.add(filevo);}TransactionManager.commitTransaction();} catch (Exception e) {TransactionManager.rollbackTransaction();this.setMessage("保存失败!", ErrorLevel.ERROR);e.printStackTrace();}return "gzd";}/** * Ajax 删除上传文件 * @return */public String deleteFile() {String[] lshArray = lshs.split(",");for (int i = 0; i < lshArray.length; i++) {try {FileFactory.getFileFactory().remove(lshArray[i]);} catch (DataAccessException e) {e.printStackTrace();} catch (ObjectNotExistException e) {e.printStackTrace();}}messageType = Constants.SUCCESS;return "deleteOK";}/** * 下载文件 * @return */public String showFile() {getInputStream();return null;}// 如果下载文件名为中文,进行字符编码转换public String getDownloadFileName() {String downFileName = fileName;try {downFileName = new String(downFileName.getBytes(), "ISO8859-1");} catch (UnsupportedEncodingException e) {e.printStackTrace();}return downFileName;}// 从下载文件原始存放路径读取得到文件输出流public void getInputStream() {FilePO filePO = null;InputStream inputStream = null;try {HttpServletResponse response = ServletActionContext.getResponse();OutputStream out = response.getOutputStream();filePO = (FilePO) FileFactory.getFileFactory().get(lsh);fileName = filePO.getCy_fsclmc();response.setContentType(filePO.getCy_wjlx());//文件内容((HttpServletResponse) response).setHeader("Content-Disposition","attachment; filename="" + Utf8Util.toUtf8String(fileName)+ """);out.write(PxjgfjPO0.getB());//字节// 关闭输出流if (null != out) {out.flush();out.close();}} catch (Exception e) {e.printStackTrace();}}}???
Dao:
public void add(POvo PO)throws DataAccessException, ObjectAlreadyExistException {PxjgfjPO po = (PxjgfjPO) pxjgfjPO;JDBCDataSource dataSource = getJDBCDataSource();PreparedStatement preparedStatement = null;try {logger.debug("执行SQL:" + PREPARED_INSERTSQL);preparedStatement =dataSource.getConnection().prepareStatement(PREPARED_INSERTSQL);preparedStatement.setString(1, po.getCy_lsh());preparedStatement.setString(2, po.getCy_wjlx());preparedStatement.setBinaryStream(3, po.getCy_wjnr(),po.getCy_wjnr().available());preparedStatement.executeUpdate();} catch (SQLException e) {throw new DataAccessException(e);} catch (IOException e) {e.printStackTrace();} finally {if (preparedStatement != null) {try {preparedStatement.close();} catch (SQLException e1) {e1.printStackTrace();}}}}public Object build(ResultSet resultSet) throws DataAccessException {PxjgfjPO pxjgfjPO = new PxjgfjPO();try{pxjgfjPO.setCy_wjnr(resultSet.getBinaryStream("CY_WJNR"));//用字节B来代替pxjgfjPO.setB(resultSet.getBytes("CY_WJNR"));} catch (SQLException e) {throw new DataAccessException(e);}return pxjgfjPO;}
???
vo:
public class FilePO implements Serializable {public final static String TABLE_NAME = "pxjg_fj"; private byte[] B;private String lsh;private String wjlx;/** 文件内容: 0-当前照片1-历史照片 */private InputStream wjnr;public String getLsh(){return this.lsh;}public void setLsh(String lsh){this.lsh =lsh;}public String getWjlx(){return this.wjlx;}public void setWjlx(String wjlx){this.wjlx =wjlx;}public InputStream getWjnr(){return this.cy_wjnr;}public void setWjnr(InputStream wjnr){this.wjnr = wjnr;}public byte[] getB() {return b;}public void setB(byte[] b) {this.b = b;}}
?
?