Correct attaching specified file to existing request.

@param file    to attach
@param request to attach at

@return request with attached file

@throws IOException iff problems with accessing file
969662
public static OAuthRequest attachFile(File file, OAuthRequest request) throws IOException {
        String boundary = generateBoundaryString();
        request.addHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
        request.addBodyParameter("file", file.getName());

        StringBuilder boundaryMessage = new StringBuilder("--");
        boundaryMessage.append(boundary)
                .append("\r\n")
                .append("Content-Disposition: form-data; name=\"file\"; filename=\"").append(file.getName())
                .append("\"\r\n")
                .append("Content-Type: ").append("application/x-unknown")
                .append("\r\n\r\n");

        String endBoundary = "\r\n--" + boundary + "--\r\n";

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        buffer.write(boundaryMessage.toString().getBytes(CHARSET_NAME));
        buffer.write(Files.readFile(file));
        buffer.write(endBoundary.getBytes(CHARSET_NAME));
        request.addPayload(buffer.toByteArray());
        buffer.close();

        return request;
    }
1036850
public UpdateResponse update(UpdateRequest request) throws IOException {
        return new UpdateResponse(client.doPost(request.toUrl(), request.getBody()));
    }
194160
public static void addFile(String s) throws IOException {
        File f = new File(s);
        addFile(f);
    }
792338
@RequestMapping(value = "/v1/upload",
            method = RequestMethod.POST,
            consumes = "multipart/form-data")
    @ResponseBody
    public final ApiHead upload(
            @RequestParam("file") final MultipartFile file) {
        if (file == null) {
            logger.info("in file upload: file is null");
            return null;
        }
        logger.info("in file upload: " + file.getOriginalFilename());
        storageService.store(file);
        final String name =
                file.getOriginalFilename().replaceAll("\\.ged", "");
        return crud().readOne(name, "");
    }
1084930
public void addFile(String path, File file) throws IOException {
    addFile(path, file, null);
  }
325231
public void restoreFromFile(File file) throws IOException
    {
        byte[] rawData = new byte[(int) file.length()];
        ByteBuffer bufData = null;
        FileInputStream fis = null;
        DataInputStream dis = null;
        try {
            fis = new FileInputStream(file);
            dis = new DataInputStream(fis);
            dis.readFully(rawData);
            bufData = ByteBuffer.wrap(rawData);
            restoreFromBuffer(bufData);
        }
        finally {
            if (dis != null) {
                dis.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
    }
725812
public ArticleAttachments createUploadArticle(long articleId, File file) throws IOException {
        return createUploadArticle(articleId, file, false);
  }
182719
public static String file(File file) throws IOException {
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            return stream(in, file.length());
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
170978
protected void doHandleFileAjaxActionRequest(final Request request) {
		// Protect against client-side tampering of disabled components
		if (isDisabled()) {
			throw new SystemException("File widget is disabled.");
		}

		// Check for file id
		String fileId = request.getParameter(FILE_UPLOAD_ID_KEY);
		if (fileId == null) {
			throw new SystemException("No file id provided for ajax action.");
		}

		// Check valid file id
		FileWidgetUpload file = getFile(fileId);
		if (file == null) {
			throw new SystemException("Invalid file id [" + fileId + "].");
		}

		// Run the action
		final Action action = getFileAjaxAction();
		if (action == null) {
			throw new SystemException("No action set for file ajax action request.");
		}

		// Set the selected file id as the action object
		final ActionEvent event = new ActionEvent(this, "fileajax", fileId);
		Runnable later = new Runnable() {
			@Override
			public void run() {
				action.execute(event);
			}
		};

		invokeLater(later);
	}
961976
protected ServletFileUpload createServletFileUpload(final HttpServletRequest request) {
        final DiskFileItemFactory fileItemFactory = createDiskFileItemFactory();
        final ServletFileUpload upload = newServletFileUpload(fileItemFactory);
        upload.setHeaderEncoding(request.getCharacterEncoding());
        upload.setSizeMax(getSizeMax());
        return upload;
    }
1071152
public static Path getFile(HttpServletRequest request)
            throws IOException {
        Path result = null;

        // Set up the objects that do all the heavy lifting
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);

        try {
            // Read the items - this will save the values to temp files
            for (FileItem item : upload.parseRequest(request)) {
                if (!item.isFormField()) {
                    result = Files.createTempFile("upload", ".file");
                    item.write(result.toFile());
                }
            }
        } catch (Exception e) {
            // item.write throws a general Exception, so specialise it by wrapping with IOException
            throw new IOException("Error processing uploaded file", e);
        }

        return result;
    }
1318945
public void addFile(File file) {
        if (file != null && file.exists()) {
            try {
                classPath.add(file.toURI().toURL());
            } catch (MalformedURLException e) {
                throw new AssertionError("converting an existing file to an url should have never thrown an exception!");
            }
        }
    }
351365
public File write(File file) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(file)) {
            write(fos);
            return file;
        }
    }
478267
protected void doHandleUploadRequest(final Request request) {
		// Protect against client-side tampering of disabled/read-only fields.
		if (isDisabled() || isReadOnly()) {
			throw new SystemException("File widget cannot be updated.");
		}

		// Only process on a POST
		if (!"POST".equals(request.getMethod())) {
			throw new SystemException(
					"File widget cannot be updated by " + request.getMethod() + ".");
		}

		// Check only one file item in the request
		FileItem[] items = request.getFileItems(getId());
		if (items.length > 1) {
			throw new SystemException("More than one file item received on the request.");
		}

		// Check the client provided a fileID
		String fileId = request.getParameter(FILE_UPLOAD_MULTI_PART_ID_KEY);
		if (fileId == null) {
			throw new SystemException("No file id provided for file upload.");
		}

		// Wrap the file item
		FileItemWrap wrap = new FileItemWrap(items[0]);
		
		// if fileType is supplied then validate it
		if (hasFileTypes() && !FileUtil.validateFileType(wrap, getFileTypes())) {
			String invalidMessage = FileUtil.getInvalidFileTypeMessage(getFileTypes());
			throw new SystemException(invalidMessage);
		}

		// if fileSize is supplied then validate it
		if (hasMaxFileSize() && !FileUtil.validateFileSize(wrap, getMaxFileSize())) {
			String invalidMessage = FileUtil.getInvalidFileSizeMessage(getMaxFileSize());
			throw new SystemException(invalidMessage);
		}
		
		FileWidgetUpload file = new FileWidgetUpload(fileId, wrap);
		addFile(file);

		// Set the file id to be used ion the renderer
		setFileUploadRequestId(fileId);
		setNewUpload(true);
	}
267492
protected void doHandleThumbnailRequest(final FileWidgetUpload file) {
		// Create thumb nail (if required)
		if (file.getThumbnail() == null) {
			Image thumbnail = createThumbNail(file.getFile());
			file.setThumbnail(thumbnail);
		}
		ContentEscape escape = new ContentEscape(file.getThumbnail());
		throw escape;
	}
493819
public void writeToFile(File file) {
        try {
            try (OutputStream os = new FileOutputStream(file)) {
                InputStreams.transferTo(body(), os);
            }
        } catch (IOException e) {
            throw new RequestsException(e);
        } finally {
            close();
        }
    }
781377
private static InputStream openResource(final String file) throws IOException {
		InputStream stream = HtmlConverterApplication.class.getResourceAsStream("/" + file);
		if (stream == null) {
			throw new FileNotFoundException(file);
		} else {
			return stream;
		}
	}