smartupload实现文件上传时获取表单数据(推荐)
2019/6/27 21:34:52
本文主要是介绍smartupload实现文件上传时获取表单数据(推荐),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
实现文件上传的form表单必须满足两个条件:method="post" enctype="multipart/form-data"
表单中enctype="multipart/form-data"的意思是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据。enctype="multipart/form-data"是上传二进制数据; form里面的input的值以二进制的方式传过去。所以request就得不到值了, 也就是说加了这段代码,用request就会传递不成功。
取表单字段值时,用下面的方式:
SmartUpload su = new SmartUpload(); //新建一个SmartUpload对象 su.getRequest().getParameterValues(String name); //取数组值 su.getRequest().getParameter(String name); //取单个参数单个值
注:在使用SmartUpload时需要添加相应的jar包
String softname=su.getRequest().getParameter("softname");
注意:一定要在su.upload();,之后使用,才可以获得值!!
<span style="font-size:14px;">public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { SmartUpload mySmartUpload = new SmartUpload(); try{ // Initialization mySmartUpload.initialize(config,request,response); mySmartUpload.setMaxFileSize(10*1024*1024);//限制上传文件的大小 //mySmartUpload.setAllowedFilesList("txt,html,jpg,js");//设置允许上传的文件类型 mySmartUpload.setDeniedFilesList("exe,doc");//设置禁止上传的文件列表 mySmartUpload.upload(); String hString = mySmartUpload.getRequest().getParameter("name1"); //System.out.println(hString); //System.out.println(mySmartUpload.getFiles().getCount()); for(int i = 0 ;i <mySmartUpload.getFiles().getCount();i++){//多个文件的上传 File file = mySmartUpload.getFiles().getFile(i); if(file.getSize()!=0){ //拼凑上传文件的新名称 String fileNameString = System.currentTimeMillis()+"."+file.getFileExt(); //通过servlet的实际路径拼凑上传文件的保存路径,实际使用需要修改此路径 String path = "/upload123123"+java.io.File.separator+fileNameString; file.saveAs(path); } Thread.sleep(100); /*下载文件的语句 mySmartUpload.downloadFile("/路径"+"文件名称");*/ } }catch(Exception e){ e.printStackTrace(); } }</span>
这篇关于smartupload实现文件上传时获取表单数据(推荐)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist
- 2024-03-11icons for vue
- 2024-03-07breadcrumbs react js
- 2024-03-06react login page example
- 2024-03-06react router uselocation
- 2024-03-04postgres jsonb_set