JSP批量上传方法示例 jspsmartUpload

HTML页面: form action=UploadImages method=post enctype=multipart/form-data name=form1 TABLE TR TD 酒店图片:/td td id=file input type=file name=file1 input type=button value=增加图片 οnclick=AddMore() /td /tr input type=submit name=Submit
  

HTML页面:

<form action="UploadImages" method="post" enctype="multipart/form-data" name="form1">
<TABLE >
        
        <TR >
             <TD >酒店图片:</td>
            <td id="file" >
            <input type="file" name="file1">
            <input type="button" value="增加图片" οnclick="AddMore()">
            </td>
        </tr>
                   <input type="subm<a href="http://www.999it%3Ca%20href%3D/" http:="" www.999it.net="" a="" kfyy="" netjs="" '="" target="_blank" style="padding: 0px; margin: 0px; color: rgb(37, 110, 177); text-decoration: none; ">.net' target='_blank'>it" name="Subm<a href="http://www.999it%3Ca%20href%3D/" http:="" www.999it.net="" a="" kfyy="" netjs="" '="" target="_blank" style="padding: 0px; margin: 0px; color: rgb(37, 110, 177); text-decoration: none; ">.net' target='_blank'>it" value="上传">
               
    </table>    
    </form>

<script type="text/javascript">
        function AddMore(){
            var more = document.getElementById("file");
            var br = document.createElement("br");
            var input = document.createElement("input");
            var button = document.createElement("input");
            
            input.type = "file";
            input.name = "file";
            
            button.type = "button";
            button.value = "删除";
            
            more.appendChild(br);
            more.appendChild(input);
            more.appendChild(button);
            
            button.onclick = function(){
                more.removeChild(br);
                more.removeChild(input);
                more.removeChild(button);
            }; 
        }
    </script>

 

下边用servlet来实现上传,注意servlet要跟页面处于同一路径下:

页面用POST,所以将下边内容将自动生成的覆盖掉即可:

public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

   response.setContentType("text/html;charset=gb2312");
   PrintWriter out = response.getWriter();
   SmartUpload mySmartUpload =new SmartUpload();
   long file_size_max=4000000;
   String fileName2="",ext="",testvar="";
   String url="upfileforhotel/images/";      //应保证在根目录中有此目录的存在
   //初始化
   mySmartUpload.initialize(this.getServletConfig(),request,response);
   //只允许上载此类文件
   try {
   mySmartUpload.setAllowedFilesList("jpg,gif");
   //上载文件 
  
   mySmartUpload.upload();
   //mySmartUpload.save("/upfileforhotel/images/");
   } catch (Exception e){
  
   out.print("<script>");
   out.println("alert('只允许上传.jpg和.gif类型图片文件!');");
          out.print("</script>");(责任编辑:信息发布员)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.etoak.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.etoak.dao.UploaderDaoIf; import com.etoak.dao.UploaderDaoImpl; import com.etoak.po.Uploader; import com.etoak.util.UUIDGenerator; import com.jspsmart.upload.File; import com.jspsmart.upload.Files; import com.jspsmart.upload.Request; import com.jspsmart.upload.SmartUpload; public class Upload extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=gbk"); request.setCharacterEncoding("gbk"); try { // 1:引入smartupload SmartUpload su = new SmartUpload(); // 2:设置允许上传的文件的后缀名,用逗号隔开 su.setAllowedFilesList("jpg,gif,bmp,jpeg"); // 3:设置允许上传文件的大小 .这里是3m su.setMaxFileSize(3 * 1024 * 1024); // 4:初始化,接受页面传递过来的请求 su.initialize(getServletConfig(), request, response); // 5:上传 su.upload(); // 拿取Smartupload的request // 注意当我们使用了SmartUpload此jar包后 // 无法再次使用httpServletRequest这个对象来调用 // getParameter这个方法了 Request myreq = su.getRequest(); String name = myreq.getParameter("name"); String pass = myreq.getParameter("pass"); // 拿取所有上传文件的对象 Files files = su.getFiles(); // 拿取我们上传的唯一一个文件 // 0 表示索引值 File file = files.getFile(0); // 上传文件的名字 String fileName = file.getFileName(); // 上传文件的大小 int fileSize = file.getSize(); // 上传文件的后缀名 String fileExt = file.getFileExt(); // 在服务器端开辟一个路径,建立文件夹放置文件 java.io.File myfile = new java.io.File(this.getServletContext() .getRealPath("/image")); // 如果不存在此路径 if (!myfile.exists()) { // 建立此路径 myfile.mkdir(); } // 58495849584954895.jpg String fileTrueName = new UUIDGenerator().generate() + "." + fileExt; // 组合一个另存为路径 // /image/43894834830430.jpg String finalPath = "/image/" + fileTrueName; // 另存为 file.saveAs(finalPath); Uploader up = new Uploader(); up.setName(name); up.setPass(pass); up.setPicPath(finalPath); UploaderDaoIf dao = new UploaderDaoImpl(); boolean flag = dao.addUp(up); if (flag) { this.getServletContext().setAttribute("up", up); response.sendRedirect("/JspDay3_upload/show.jsp"); } } catch (Exception ex) { ex.printStackTrace(); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值