2 qfdong qfdong 于 2016.05.06 17:45 提问

保存商品信息失败?有时成功有时失败,怎么回事?啊 5C
public void saveGood() {
        boolean temp = false;
        try {

            Dto dto = paraDto;
            StringUtil.xprint("param==" + paraDto);
            //处理编号
            if(optype.equals("add_good")){
               String good_id = UniqueID.getUniqueID(8, 0);
               setGood_id(good_id);
               dto.put("good_id", good_id);
            }

            StringUtil.xprint("开始上传文件==");
            FileUtils fileUtils = new FileUtils();
            String imageFolder = ServletActionContext.getServletContext().getRealPath("imageFolder");
            //String classPath = UploadGoodAction.class.getResource("/").getPath();
            //String imageFolder = CTUtils.getWebRootPaht(classPath, 1);

            List<File> items = getUploadGood();
             if(CTUtils.isNotEmpty(items)){
                 String fileName = null;
                 for (int i = 0; i < items.size(); i++) {
                    fileName = uploadGoodFileName.get(i);//真实文件名称
                    //生成一级文件夹
                    fileUtils.createFolder(imageFolder);
                    StringUtil.xprint("创建图片一级文件夹:" + imageFolder);
                    String temp_path = imageFolder + "/" + CTUtils.getCurDateNoSplit();

                    fileUtils.createFolder(temp_path);
                    StringUtil.xprint("创建图片二级文件夹:" + temp_path);

                    String file_prex = fileUtils.GetExt(fileName);
                    String filename = getGood_id()+"_"+(i+1)+ file_prex;
                    String file_path = StringUtil.getFilePath(temp_path, "imageFolder");

                    temp_path = temp_path + "/" + filename;
                    System.out.println("file_path=" + file_path);
                    System.out.println("temp_path=" + temp_path);

                    //输出文件
                    FileOutputStream fos = new FileOutputStream(temp_path);
                    FileInputStream fis = new FileInputStream(items.get(i));
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = fis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }

                    //处理文件名称
                    dto.put("good_pic"+(i+1), file_path + "/" + filename);

                 }
             }
             //保存商品
             temp = goodInfoService.operGoodInfo(dto);

        } catch (Exception e) {
            temp = false;
            e.printStackTrace();
        } finally {
            if(temp){
               this.renderHtml("<script>alert('保存商品信息成功!');window.parent.frames['foodTable'].location.href='good!getGoodInfo.ct';</script>");
            }else{
               this.renderHtml("<script>alert('保存商品信息失败!');window.parent.frames['foodTable'].location.href='good!getGoodInfo.ct';</script>");
            }
        }


高人帮助看下,是怎么回事?
保存商品信息失败?有时成功有时失败,怎么回事?啊
重启下tomcat就成功了,过段时间又失败!

1个回答

zs343961443
zs343961443   2016.05.06 17:52

用完 FileOutputStream fos = new FileOutputStream(temp_path);
FileInputStream fis = new FileInputStream(items.get(i));
关一下流。fos.close() fis.close()试一下

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!