对于enform仅能遍历一次的应对策略
对于那个只能访问一次的request,我用一个类封装两个字段List,分别是string字段和文件字段List,遍历一次request中的Item,按照不同的字段分别存在不同的List中,然后把装满字段的类返回到需要用到的页面
package get; import java.sql.Blob; public class File_ { String FieldName; Blob Value; public void setFieldName(String set){ this.FieldName=set; } public void setValue(Blob set){ this.Value=set; } public String getFieldName(){ return this.FieldName; } public Blob getValue(){ return this.Value; } } package get; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import javax.servlet.http.HttpServletRequest; import javax.sql.rowset.serial.SerialBlob; import java.io.*; import java.sql.Blob; import java.sql.SQLException; import java.util.List; public class Get { public Blob f_b(File_ file){//fil_blob String FILE=""; try { InputStream InputStream=new FileInputStream(String.valueOf(file)); int bit; while((bit=InputStream.read())!=-1){ FILE=FILE+Integer.toBinaryString(bit); } InputStream.close(); } catch (IOException e) { e.printStackTrace(); } byte[] buff = FILE.getBytes(); Blob blob = null; try { blob = new SerialBlob(buff); } catch (SQLException throwables) { throwables.printStackTrace(); } return blob; } public Blob in_b(InputStream inputStream){//inputstreamתblob String FILE=""; try { InputStream InputStream=inputStream; int bit; while((bit=InputStream.read())!=-1){ FILE=FILE+Integer.toBinaryString(bit); } InputStream.close(); } catch (IOException e) { e.printStackTrace(); } byte[] buff = FILE.getBytes(); Blob blob = null; try { blob = new SerialBlob(buff); } catch (SQLException throwables) { throwables.printStackTrace(); } return blob; } public It enform_getITEM(HttpServletRequest request) throws FileUploadException,IOException {//�õ��ϸ�ҳ����ı��ֶ� It it=new It(); DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory(); ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory); servletFileUpload.setHeaderEncoding("UTF-8"); List<FileItem> fileItem = servletFileUpload.parseRequest(request); for(FileItem item:fileItem) { String name_ = item.getFieldName(); if (item.isFormField()) { String value = item.getString("UTF-8"); Text_ text=new Text_(); text.setFieldName(name_); text.setValue(value); it.t.add(text); } else { InputStream in = item.getInputStream(); Blob b=this.in_b(in); File_ file=new File_(); file.setFieldName(name_); file.setValue(b); it.f.add(file); in.close(); } } return it; } public String get_text(String FieldName,It it){ for(Text_ the:it.t){//找出无法识别的原因 if(FieldName.equals(the.getFieldName())){ return the.getValue(); } } return ""; } public Blob get_file(String FieldName,It it){ for(File_ the:it.f){ if(FieldName.equals(the.getFieldName())){ return the.getValue(); } } return null; } // public Blob enform_Blob_get(HttpServletRequest request, String get) throws IOException,FileUploadException {//�õ��ϸ�ҳ���ļ��ֶ� // DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory(); // ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory); // servletFileUpload.setHeaderEncoding("GBK"); // List<FileItem> fileItem = servletFileUpload.parseRequest(request); // for(FileItem item:fileItem) { // String name_ = item.getFieldName(); // if (!item.isFormField()) { // if (get.equals(name_)) { // InputStream in = item.getInputStream(); // Blob b=this.in_b(in); // in.close(); // return b; // } // } // } // return null; // } } package get; import java.util.ArrayList; import java.util.List; public class It { public List<File_> f; public List<Text_> t; public It(){ f=new ArrayList<>(); t=new ArrayList<>(); } } package get; public class Text_ { private String FieldName; private String Value; public void setFieldName(String set){ this.FieldName=set; } public void setValue(String set){ this.Value=set; } public String getFieldName(){ return this.FieldName; } public String getValue(){ return this.Value; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!