骐源 2016-05-06 09:45 采纳率: 0%
浏览 1144
已结题

保存商品信息失败?有时成功有时失败,怎么回事?啊

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条回答 默认 最新

  • 人称帅哥 2016-05-06 09:52
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!