• 河北重大技术需求开发第七版开发第八天


    今天完成了一个上传文件的功能

     在网页点击上传可以将文件上传到java文件下

     

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html; charset=UTF-8");
            boolean isMultipart=ServletFileUpload.isMultipartContent(request);
            if(isMultipart)
            {
                FileItemFactory factory=new DiskFileItemFactory();
                ServletFileUpload upload=new ServletFileUpload(factory);
                try {
                    List<FileItem> items=upload.parseRequest(request);
                    Iterator<FileItem> it = items.iterator();
                    while(it.hasNext())
                    {
                        FileItem item = it.next();
                        String itemname = item.getFieldName();
                        int sno=-1;
                        String sname=null;
                        
                        if(item.isFormField())
                        {
                            if(itemname.equals("sno"))
                            {
                                sno=Integer.parseInt(item.getString("utf-8"));
                            }else if(itemname.equals("sname"))
                            {
                                sname=item.getString("utf-8");
                                sname=item.getName();
                            }else
                            {
                                System.out.println("其他字段");
                            }
                        }else
                        {
                            String filename=item.getName();
                            String path=request.getSession().getServletContext().getRealPath("download");
                            //String path="E:\upload";
                            //System.out.println(path);
                            
                            File file=new File(path,filename);
                            item.write(file);
                            request.setAttribute("news", filename+"上传成功,谢谢分享");
                            request.getRequestDispatcher("print.jsp").forward(request, response);
                            System.out.println(filename+"上传成功!!!");
                            return;
                        }
                        
                    }
                } catch (FileUploadException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
     
     
  • 相关阅读:
    编辑器1.0
    react native定报预披项目知识点总结
    placeholder中文字添加换行方法
    ios端position为fixed失效的解决办法
    利用fis3构建前端项目工程
    webpack学习记录
    webpack学习记录-认识loader(二)
    webpack学习记录-初步体验(一)
    去抖(节流)
    css布局:左边定宽、右边自适应
  • 原文地址:https://www.cnblogs.com/520520520zl/p/13881347.html
Copyright © 2020-2023  润新知