使用的是apache common下的fileupload

解决方案 »

  1.   

    FormFile workPhoto = form.getWorkPhoto(); //取得上传的文件
            FormFile lifePhoto = form.getLifePhoto();
            String filePath = httpServletRequest.getSession().getServletContext().getRealPath("/");
            if(workPhoto!=null){
                try{
                    InputStream stream = workPhoto.getInputStream(); //把文件读入
                    /*
                     * 建立一个上传文件的输出流 如果是linux系统请把UploadFiles后的"\\"换成"/"                OutputStream bos = new FileOutputStream(filePath +
                            "lwuser\\fileUpload\\"+fileName);                    int bytesRead = 0;
                        byte[] buffer = new byte[8192];
                        while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
                        {
                            bos.write(buffer, 0, bytesRead);//将文件写入服务器
                        }
                        bos.close();
                        stream.close();
                        photoPath[0]="\\lwuser\\fileUpload\\"+fileName;            }catch(FileNotFoundException ex){
                    System.out.println( "文件上传失败: "+ex);
                    throw new FileNotFoundException(ex.toString());
                }catch(IOException ex){
                    System.out.println( "文件上传失败: "+ex);
                    throw new FileNotFoundException(ex.toString());
                }
            }
    这里只是片段,仅供参考!