| « | November 2025 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | | | |
| 公告 |
|
| Blog信息 |
|
blog名称: 日志总数:22 评论数量:55 留言数量:0 访问次数:130148 建立时间:2006年3月13日 |

| |
|
多文件上传(服务器存文件) 软件技术
jjs_love 发表于 2007/3/22 10:04:08 |
| jsp页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %><%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %><%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %><%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested" %>
<html:html locale="true"> <head> <html:base />
<title>广州军区房地产管理局图片视频上传系统</title>
<style type="text/css"><!--body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px;}.style { border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #afc4d9;}
.file { font: 10pt; color: black; }--></style><link href="css/style.css" rel="stylesheet" type="text/css"><SCRIPT>function CheckFile(str){
var reg=/^[A-Z]{1}\:\\(?!\s+)([^\\]+(?:\\))*([^\s]+(?:\.))+[^\.\s]+$/;return Check(reg,str);
}
</SCRIPT></head><html:errors/><body><html:form method="post" action="ks.do?method=upload" enctype="multipart/form-data"> <table width="760" border="0" align="center" cellpadding="0" cellspacing="0">
<tr> <td align="left" valign="top"><table width="760" border=1 align="center" cellpadding=0 bordercolorlight=#afc4d9 bordercolordark=#ffffff bgcolor=#ffffff class=p1> <tr> <td width="1001" height="25" align="center" bgcolor="#CADDFD"><span class="bt">添加试题文件</span></td> </tr> </table> <table width="100%" border=1 cellpadding=0 bordercolorlight=#afc4d9 bordercolordark=#ffffff bgcolor=#ffffff class=p1 id=Table4> <tbody>
<tr align="center" valign="middle"> <td height="20">文件列表</td> <td height="20" align="left" valign="top"> <div id="fileDiv" style='height:150px;top:150px;left:0px;overflow-x:auto;overflow-y:auto ;border-style:outset;border-width:0pt;border-color: black;'> <nested:iterate id="uploadFile" property="uploadfiles" name="imgForm" indexId="index"> <nested:nest property="uploadFile"> 文件名称:<nested:text property="mc" name="uploadFile" indexed="true"/> 选择文件:<nested:file property="theFile" name="uploadFile" indexed="true"/> </nested:nest> </nested:iterate> <table id=conditionTable border=0 > </table> </div> </td> </tr> </table> <table width="760" > <tr> <td width="760" height="26" align="center" valign="middle" bgcolor="#CADDFD" class="style"> <table width="49%" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="right" valign="center"> <input type="button" name="btnAddFile" value="继续添加" onclick="addFile()"/> <input type="submit" name="submit" value="上 传" /> <input type="button" name="btnBack" value="返 回" onclick="javascript :window.location.href='http://37.51.176.64/House'" /> </td> </table></td> </tr> </table></td> </tr> </table></html:form> </body> </html:html>
<script language="javascript">
var i=0; function addFile() { i++; currRow=conditionTable.insertRow(); cellc=currRow.insertCell(); cellcContext= ' 文件名称:<input type="text" name="uploadFile['+i+'].mc" >选择文件:<input type="file" name="uploadFile['+i+'].theFile" > <button onclick="removeFile();">删除</button><br>'; cellc.innerHTML=cellcContext;
} function findTD(o){if (o.nodeName=="TR"||o.nodeName=="TABLE") return;if(o.nodeName=="TD")return (o);elsereturn (o.parentElement);}
function removeFile(){ o = findTD(event.srcElement); conditionTable.deleteRow(o.parentElement.rowIndex*1); i--;}
function MutilUpload(){ var cnt; var bl=true; for(cnt=0;cnt<=i;cnt++){ bl=bl&&checkOneFile(cnt);
} if(bl){ document.forms[0].submit(); return bl; }else return bl;} function checkOneFile(j){
var eFile=document.getElementsByName("uploadFile["+j+"].theFile").item(0).value; if(eFile==''||eFile==null||!CheckFile(eFile)){ alert('文件输入错误'+eFile); return false; }else return true;}
</script>
formbean
package cn.com.house.view.struts.form;
import java.util.ArrayList;import java.util.List;
import org.apache.struts.action.ActionForm;import org.apache.struts.upload.FormFile;
import cn.com.house.data.bo.Ks;
public class ImagForm extends ActionForm { private Ks ksvo=new Ks(); private List uploadfiles; private FormFile fileContent; public ImagForm(){ uploadfiles=new ArrayList(); uploadfiles.add(new UploadFile()); }
public UploadFile getUploadFile(int index) { int size = uploadfiles.size(); if(index>0){ uploadfiles.add(new UploadFile()); size++; } return (UploadFile)uploadfiles.get(size-1); }
public void setUploadFile(List uploadfiles) { this.uploadfiles = uploadfiles; } public List getUploadfiles() { return uploadfiles; }
public Ks getKsvo() { return ksvo; }
public void setKsvo(Ks ksvo) { this.ksvo = ksvo; }
public FormFile getFileContent() { return fileContent; }
public void setFileContent(FormFile fileContent) { this.fileContent = fileContent; }
}
Action
package cn.com.house.view.struts.action;
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;import java.net.URLDecoder;import java.text.SimpleDateFormat;import java.util.List;import java.util.Random;import java.util.UUID;
import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.actions.DispatchAction;import org.apache.struts.upload.FormFile;
import cn.com.house.data.bo.Ks;import cn.com.house.data.dao.impl.ArmynewsDAO;import cn.com.house.view.struts.form.ImagForm;
import cn.com.house.view.struts.form.UploadFile;
public class KsAction extends DispatchAction{ private final static String hql="from Ks as k where k.type=? order by k.id asc"; private ArmynewsDAO armynewsDAO; private String filename; private String filenamereal; public ArmynewsDAO getArmynewsDAO() { return armynewsDAO; }
public void setArmynewsDAO(ArmynewsDAO armynewsDAO) { this.armynewsDAO = armynewsDAO; } public ActionForward kslist( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { saveToken(request); String type=request.getParameter("type"); List list=armynewsDAO.findAllObj(hql,"1"); request.setAttribute("list",list); String path="/ks/kslist.jsp?type="+type; return new ActionForward(path); } public ActionForward imgupload( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { saveToken(request);
return new ActionForward("/ks/addimag.jsp"); } public ActionForward del( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { String id=request.getParameter("id"); armynewsDAO.delKs(new Long(id),getUploadPath()); String path="/ks.do?method=kslist&type=1"; return new ActionForward(path); } public ActionForward upload( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
{ ImagForm formvo=(ImagForm) form; if(isTokenValid(request,true)){ List list=formvo.getUploadfiles(); int size=list.size(); String mc=""; for(int i=0;i<size;i++){ FormFile file=((UploadFile) list.get(i)).getTheFile(); mc=((UploadFile) list.get(i)).getMc(); if(file==null){ }else{ uploadfile(file); if(mc==null|| mc.equals("")){ saveDB(this.getFilenamereal(),"1"); }else{ saveDB(mc,"1"); } } } }
String path="/ks.do?method=kslist&type=1"; return new ActionForward(path); } public ActionForward ks( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
saveToken(request); return new ActionForward("/ks/ks.jsp"); } public ActionForward reslut( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
ImagForm formvo=(ImagForm) form; try{ if(isTokenValid(request,true)){ FormFile file= formvo.getFileContent(); String mc=""; mc=request.getParameter("mc"); uploadfile(file); if(mc==null|| mc==""){ saveDB(this.getFilenamereal(),"2"); }else{ saveDB(mc,"2"); } request.setAttribute("result","ok"); }else{ request.setAttribute("result","token"); } }catch(Exception e){ request.setAttribute("result","no"); } return new ActionForward("/ks/reslut.jsp"); } public ActionForward reslutlist( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { saveToken(request); List list=armynewsDAO.findAllObj(hql,"2"); request.setAttribute("list",list); return new ActionForward("/ks/reslutlist.jsp"); } public ActionForward delreslut( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { String id=request.getParameter("id"); armynewsDAO.delKs(new Long(id),getUploadPath()); return reslutlist(mapping,form,request,response); }
public ActionForward getXls( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { try{ String file=request.getParameter("file"); String path=this.getUploadPath()+"/ks/Upload/"+file; response.reset(); response.setContentType("application/vnd.ms-excel"); InputStream ips =new FileInputStream(path); OutputStream ops=response.getOutputStream(); int data=-1; while((data = ips.read())!=-1) { ops.write(data); } ops.flush(); ips.close(); }catch(Exception e){ e.printStackTrace(); }
return null;//kslist(mapping,form,request,response); }
public void uploadfile(FormFile files) { PrintWriter buff=null;
try{ String formart = "yyyyMMddHHmmss"; Random random = new Random(); String fileName = new SimpleDateFormat(formart).format(new java.util.Date()); fileName += Math.abs(random.nextInt()) % 100; String rndstr=fileName; //String rndstr=UUID.randomUUID().toString(); String laststr=files.getFileName().substring(files.getFileName().lastIndexOf(".")); String pst=files.getFileName().substring(0,files.getFileName().lastIndexOf(".")); this.setFilenamereal(pst); String filename=files.getFileName().substring(0,files.getFileName().lastIndexOf(".")).concat(rndstr)+laststr; this.setFilename(filename); File newdir=new File(ArmynewsAction.getUploadPath()+"/ks/Upload/"); if(!newdir.exists()){ newdir.mkdir(); } String path=ArmynewsAction.getUploadPath()+"/ks/Upload/"+filename; File savefile=new File(path); final int size=8192; InputStream input=files.getInputStream(); BufferedInputStream buffinput=new BufferedInputStream(input); FileOutputStream fileout=new FileOutputStream(savefile); BufferedOutputStream buffout=new BufferedOutputStream(fileout); byte []buffer=new byte[size]; while(buffinput.read(buffer,0,size)!=-1){ buffout.write(buffer,0,size); } buffout.close(); fileout.close(); buffinput.close(); input.close(); }catch(Exception e){ e.printStackTrace(); }
}
public static String getUploadPath(){ String testStr=ArmynewsAction.class.getClassLoader().getResource("").getPath(); testStr=testStr.substring(0,testStr.lastIndexOf("/")); testStr=testStr.substring(0,testStr.lastIndexOf("/")); testStr=testStr.substring(0,testStr.lastIndexOf("/")); testStr=URLDecoder.decode(testStr); return testStr; } public void saveDB(String mc,String type){ Ks kss=new Ks(); kss.setPath(this.getFilename()); kss.setType(type); kss.setMc(mc); armynewsDAO.saveObj(kss); }
public String getFilename() { return filename; }
public void setFilename(String filename) { this.filename = filename; }
public String getFilenamereal() { return filenamereal; }
public void setFilenamereal(String filenamereal) { this.filenamereal = filenamereal; } public ActionForward test( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { armynewsDAO.delKs(new Long(1),getUploadPath()); return null; } } |
|
|