OutputStream streamOut = new FileOutputStream(dir + "/"+fname);fname

解决方案 »

  1.   

    FormFile file = noticeEditForm.getAttachFile();//取得上传的文件
    if(null != file && null != file.getFileName() && 0 != file.getFileName().trim().length()){
    String fileName = Feedback.getFormatedCurrentDate(conn) + staff.getId() + StringUtil.getExtendName(file.getFileName());
    try {
    InputStream stream = file.getInputStream();//把文件读入
    String filePath = servlet.getServletContext().getRealPath("/");//取当前系统路径
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStream bos = new FileOutputStream(filePath + "attachment\\" + fileName);//建立一个上传文件的输出流.
    notice.setIsAnnex("1");
    notice.setFilePath("./attachment/" + 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();
    }catch(Exception e){
    errors.add("uploadFail", new ActionError("error.bulletin.uploadFail"));
    e.printStackTrace();
    }
    }else{
    //errors.add("fileRequired", new ActionError("error.bulletin.fileRequired"));
    }
      

  2.   


    public ActionForward execute(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
        throws Exception {        String dir=servlet.getServletContext().getRealPath("/upload");        HtmlFileForm hff = (HtmlFileForm) form;        // org.apache.struts.upload.FormFile contains the uploaded file
            FormFile file = hff.getFile();        // If no file was uploaded (e.g. first form load), then display View
            if (file == null ) {
                    return mapping.findForward("success");        }        // Get the name and file size
            String fname = file.getFileName();
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
             java.util.Date  mydate=new java.util.Date();
            String s=sdf.format(mydate);
            fname  = s + fname .substring(fname.indexOf("."));
            String size = Integer.toString(file.getFileSize()) + " bytes";        InputStream streamIn = file.getInputStream();
            OutputStream streamOut = new FileOutputStream(dir + "/"+fname);        int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
                streamOut.write(buffer, 0, bytesRead);
            }        streamOut.close();
            streamIn.close();
            // Populate the form bean with the results for display in the View
            hff.setFname(fname);
            hff.setSize(size);        // Clean up our toys when done playing
            file.destroy();        // Forward to default display
            return mapping.findForward("success");    }