qq_30435609 2015-10-28 07:56
浏览 569
已结题

那个大神帮我更改成可实现上传3张图片的

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%@page import="java.io.File"%>
<%@page import="cn.to8ge.entity.*"%>
<%@page import="cn.to8ge.biz.*"%>
<%@page import= "java.text.SimpleDateFormat" %>
<%@page import= "java.util.Date" %>
<%@page import="org.apache.commons.fileupload.*"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">



<%
  to_new   tcc=new to_new();
  newBiz   nb=new newBiz();
  String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
  tcc.setNew_time(time);
  String   fieldName="";
  boolean   isMultipart=ServletFileUpload.isMultipartContent(request);
  String   uploadFilePath=getServletContext().getRealPath("upload");
  if(isMultipart){
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setSizeMax(217*183*3);

        try{
        List<FileItem> items = upload.parseRequest(request);
            Iterator<FileItem> iter = items.iterator();

            while(iter.hasNext()) {   
               FileItem item = (FileItem) iter.next();
                if (item.isFormField()){  
                    fieldName = item.getFieldName();                        
                    if (fieldName.equals("ntid")){   
                    tcc.setNew_classify(item.getString("UTF-8"));
                    }else if(fieldName.equals("ntitle")){            
                    tcc.setNew_title(item.getString("UTF-8"));
                    }else if(fieldName.equals("daodu")){             
                    tcc.setNew_takeaway(item.getString("UTF-8"));
                    }else if(fieldName.equals("nauthor")){           
                    tcc.setNew_author(item.getString("UTF-8"));
                    }else if(fieldName.equals("ncontent")){              
                    tcc.setNew_content(item.getString("UTF-8"));
                    }else if(fieldName.equals("ncontent2")){             
                    tcc.setNew_content2(item.getString("UTF-8"));
                    }else if(fieldName.equals("ncontent")){              
                    tcc.setNew_content(item.getString("UTF-8"));
                    }else if(fieldName.equals("city")){              
                    tcc.setNew_city(item.getString("UTF-8"));
                    }
        }else{


            String fileName = item.getName();
                    List<String> filType=Arrays.asList("gif","jpg","jpeg");
                    String ext=fileName.substring(fileName.lastIndexOf(".")+1);
                    if(fileName!=null&&!fileName.equals("")&&!filType.contains(ext)){  //判断文件类型是否在允许范围内

%>
       <script type="text/javascript">
                        alert("上传失败,文件类型只能是gif、jpg、jpeg");
                        location.href = "./companyadmin/Updatestoce.jsp";
                    </script>
      <%   
      }else if (fileName != null && !fileName.equals("")) {
                        File fullFile = new File(time+item.getName());

                        File saveFile = new File(uploadFilePath, fullFile.getName());

                        item.write(saveFile);

                        tcc.setNew_picture(fullFile.getName());


                    }
                }       
            }
        }catch(FileUploadBase.SizeLimitExceededException ex){

        %>      
         <script type="text/javascript">
                        alert("上传失败,文件太大,单个文件的最大限制是:3MB");
                        location.href = "./companyadmin/Updatestoce.jsp";
                    </script>   

                    <%
        }           
    }       
    if(nb.insertnewinfo(tcc)==true){

%>

alert("上传成功!"); location.href = "./companyadmin/Updatestoce.jsp";
        <%
    }else{

%>

alert("上传失败!"); location.href = "../companyadmin/Updatestoce.jsp";
        <%
    }
    %>


  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 MATLAB动图的问题
    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名