使用SmartUpload实现文件的上传

使用SmartUpload实现文件的上传

今天使用了SmartUpload来实现文件的上传,发现form表单在使用了enctype=“multipart/form-data“(二进制)的情况下(默认为application/x-www-form-urlencoded),request不能直接使用getParameter()来获取前台传递的参数,于是开始再网上进行了资料的查找。

<form action="UpdateServlet" id="upload_form" method="post" enctype="multipart/form-data">
   <label for="uploadFile">代码文件:</label>
   <input type="file" name="uploadFile" id="uploadFile"/>
   <span id="uploadFile_info">
      <span class="icon-info info_text"></span>
   <span class=" info_text"> 必填,请选择不大于20M的.zip或.rar文件上传</span>
   </span><br/>
   <label for="intro">代码说明:</label>
   <input type="text" name="intro" id="intro" placeholder="代码说明"/>
   <span id="intro_info">
      <span class="icon-info info_text"></span>
      <c:choose>
           <span class="info_text"> 必填,不能长于50个字符</span>
     </span>
   <br/>
   <div class="action-bar">
      <a class="submit_btn large_btn" id="submit_btn"><span class="icon-upload"></span> 上传</a>
      <a class="grey_btn large_btn" href="admin.jsp"><span class="icon-undo2"></span> 返回</a>
   </div>
</form>

通过一段时间的查找的发现了要从request拿值要su.getRequest().getParameter(),必须在su.upload()的后面。

package com.tx.test.servlet;

import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.tx.test.characteristic.UniqueId;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.UUID;

@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置文件路径
       String filePath = "E:ran/instrument";
        File file = new File(filePath);
        if(!file.exists()){
            file.mkdirs();
        }
       //创建SmartUpload对象
       SmartUpload su = new SmartUpload();
       //初始化SmartUpload对象
       su.initialize(getServletConfig(),request,response);
       //设置文件上传的大小
       su.setTotalMaxFileSize(1024*1024*10);
       //设置文件最大值
       su.setMaxFileSize(10240*10240*100);
       //设置上传文件类型
       su.setAllowedFilesList("zip,rar");
        try {
            su.upload();
            //getFiles():获取文件
            Files files = su.getFiles();
            com.jspsmart.upload.File file1;
            String intro = su.getRequest().getParameter("intro");
            //getCount():获取文件的总数
            for(int i=0;i<files.getCount();i++){
                file1 = files.getFile(i);
               //getFileExt():获取文件扩展名 file1.saveAs(filePath+File.separator+UniqueId.getId()+UUID.randomUUID()+"."+file1.getFileExt());
            }
        }catch (Exception e){
            System.out.println("文件上传失败!!!");
        }
        request.getRequestDispatcher("upload.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }
}

参考文章:

使用commons-fileupload的jar包获取request的值方法

java代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值