问题描述
两个jsp:1.NewList.jsp、2.MyJsp.jsp-----------------NewList.jsp---------------原代码<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%><%Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><basehref="<%=basePath%>"><title></title><metahttp-equiv="pragma"content="no-cache"><metahttp-equiv="cache-control"content="no-cache"><metahttp-equiv="expires"content="0"><metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"><metahttp-equiv="description"content="Thisismypage"><!--<linkrel="stylesheet"type="text/css"href="styles.css">--><scripttype="text/javascript">functioncallback(msg){document.form1.file2.value=msg;}functioncallback1(msg){document.form1.file3.value=msg;}functioncallback2(msg){document.form1.filesize.value=msg;}</script></head><body><formaction="MyJsp.jsp"method="post"name="form1"enctype="multipart/form-data"target="ifm"><iframeid='ifm'name='ifm'style="display:none"></iframe><tablewidth="65%"align="center"height="50%"cellpadding="0"cellspacing="0"border="1"><trheight="24"><tdwidth="25%">单词名称</td><tdwidth="75%"><inputtype="text"name="word"/></td></tr><trheight="24"><tdwidth="25%"align="right">上传文件</td><tdwidth="75%"><inputname="file1"type="file"><inputtype="button"name="button"value="点击上传"></td></tr><trheight="24"><tdwidth="25%">原始文件名</td><tdwidth="75%"><inputtype="text"name="file2"id="file2"/></td></tr><trheight="24"><tdwidth="25%">改后文件名</td><tdwidth="75%"><inputtype="text"name="file3"id="file3"/></td></tr><trheight="24"><tdwidth="25%">文件大小</td><tdwidth="75%"><inputtype="text"name="filesize"id="filesize"></td></tr><trheight="24"><tdwidth="25%"></td><tdwidth="75%"><inputtype="button"name="button"value="提交"/></td></tr></table></form></body></html>-----------------------------MyJsp.jsp---------------------------<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%><%@pageimport="org.apache.commons.fileupload.disk.DiskFileItemFactory"%><%@pageimport="org.apache.commons.fileupload.servlet.ServletFileUpload"%><%@pageimport="org.apache.commons.fileupload.FileItem"%><%@pageimport="java.text.SimpleDateFormat"%><%@pageimport="java.io.File"%><%Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%//获得Servlet上下文对象ServletContextcontext=this.getServletContext();//磁盘文件列表工厂对象DiskFileItemFactoryfact=newDiskFileItemFactory();//Servlet文件上传对象ServletFileUploadupload=newServletFileUpload(fact);//获得上传列表//获得最大上传对象超出10兆将会出现异常upload.setSizeMax(1024*1024*10);//获得所有上传对象集合List<FileItem>fileitem=upload.parseRequest(request);//获得第一个文件对象FileItemfile=fileitem.get(0);//获得文件大小longfilesize=file.getSize();//获得客户端文件路径StringClientName=file.getName();//获得原始文件名称StringOldFileName=ClientName.substring(ClientName.lastIndexOf("\")+1);//获得原始文件后缀StringextName=ClientName.substring(ClientName.lastIndexOf("."));//生成新文件名称(当前毫秒数1~1000)Datedate=newDate();SimpleDateFormatsimpldate=newSimpleDateFormat("yyyyMMddhhmmss");StringnewFileName=simpldate.format(date)+extName;//设置上传文件夹和临时文件夹的物理路径StringsivePath="D:/vedio";sivePath=sivePath+"/"+newFileName;System.out.println(sivePath);//sivePath=sivePath+newFileName;//创建文件对象Filef=newFile(sivePath);if(!f.exists()){f.mkdirs();}StringtargetPath=sivePath+newFileName;System.out.println(targetPath);Filetargetfile=newFile(targetPath);//写入文件(上传文件)file.write(targetfile);out.println("<script>parent.callback('"+OldFileName+"')</script>");out.println("<script>parent.callback1('"+newFileName+"')</script>");out.println("<script>parent.callback2('"+filesize+"')</script>");%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><title></title><metahttp-equiv="pragma"content="no-cache"><metahttp-equiv="cache-control"content="no-cache"><metahttp-equiv="expires"content="0"><metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"><metahttp-equiv="description"content="Thisismypage"><!--<linkrel="stylesheet"type="text/css"href="styles.css">--></head><body></body></html>