jsp+iframe+serverlet实现文件上传

实现文件上传我想大家也都会

这是jsp页面的代码

index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td colspan="2" align="center">
商品添加
</td>
</tr>
<tr>
<td>
<form action="Iframe.jsp" target="My_Iframe" method="post">
名称:&nbsp;<input  name="gname"/><br /><br />
类别:&nbsp;<input name="gtype"/><br /><br />
价格:&nbsp;<input name="gprice"/><br /><br />
数量:&nbsp;<input name="gnumber"/><br /><br />
照片:&nbsp;<input id="photo" name="gphoto"/><br /><br />


</td>
<td >
<iframe name="My_Iframe" src="Iframe.jsp?filename=1407170231464.jpg" style="height:180px">
</iframe>
</td>
</tr>
<tr>
<td colspan="2">
描述:&nbsp;<textarea style="width:450px" cols="30" rows="5"></textarea>    
</td>
</tr>
</table>
</form>
</body>

</html>

这是iframe框架所引用的界面

Iframe.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<%
   String path = request.getContextPath();
   String filename = request.getParameter("filename");
   String image = path+"/"+filename;
   
 //  if(image==null||image=="")
//   {
//       image=path+"/"+"1407170231464.jpg";
 //  }
   
   System.out.print(image);
%>
<body onload="returnParent()">
<img   src="<%=image %>" width="100px" height="100px"/>
<input id="image1" value="<%=filename %>" type="hidden"/>
<form action="image" method="post"   enctype="multipart/form-data">
    <input type="file" name="goodsImage" />
    <input type="submit" value="上传" />
</form>
<script type="text/javascript">
function returnParent() {
    var frameValue = document.getElementById("image1").value;
    //window表示当前的页面
    //window.parent表示获取到父窗体
    window.parent.document.getElementById("photo").value = frameValue;
}
</script>
</body>
</html>

这里学到了新知识就是将iframe窗体中的某个元素的值赋给父窗体

 var frameValue = document.getElementById("image1").value;
    //window表示当前的页面
    //window.parent表示获取到父窗体
    window.parent.document.getElementById("photo").value = frameValue;

下面的这个是iframe内点击上传时 anction所对应的Serverlet代码
imageservlets.java

package com.shxt.lesson16homework.Servlets;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


public class imageservlets extends HttpServlet {
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        try{
           
            
            DiskFileItemFactory factory = new DiskFileItemFactory();
             
             ServletFileUpload upload = new ServletFileUpload(factory);
             
             FileItemIterator iter = upload.getItemIterator(request);
             FileItemStream stream = iter.next();        //获取到文件(流默认是闭合的)
            InputStream input = stream.openStream();    //openStream方法将流打开,转化为标准的InputStream(字节流)
//            File f = new File(input);
//            FileInputStream fs = new FileInputStream(input);
//            String filename=stream.getName();
//            
//             String kzm = filename.substring(filename.length()-4);
             //文件名称
             String tomcatPath = request.getSession().getServletContext().getRealPath("/");
             String imgname= new java.util.Date().getTime()+".jpg";
             String newfilename =tomcatPath+"/"+ imgname;
             File file = new File(newfilename);
             FileOutputStream output = new FileOutputStream(file);
            byte[] buffer = new byte[1024];    //字节数组类型的缓冲区

            while (true) {
                //输入流input指向源,从源中读取数据,暂时存储在buffer中
                int count1 = input.read(buffer);
                System.out.println(count1);
                if (count1 == -1) {
                    break;
                }
                //输出流output指向目标,向目标写入数据,数据从buffer中获取
                output.write(buffer, 0, count1);
                
            }
            
            //System.out.print(kzm);
            response.sendRedirect("Iframe.jsp?filename="+imgname);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}

Serverlet处理上传文件时引入了3个包
commons-fileupload-1.2.2.jar  commons-io-2.0.1.jar  commons-logging.jar

 

jsp+iframe+serverlet实现文件上传,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。