private static FileItem prepareFileItemFromInputStream( PipelineContext pipelineContext, InputStream inputStream, int scope) { // Get FileItem final FileItem fileItem = prepareFileItem(pipelineContext, scope); // Write to file OutputStream os = null; try { os = fileItem.getOutputStream(); copyStream(inputStream, os); } catch (IOException e) { throw new OXFException(e); } finally { if (os != null) { try { os.close(); } catch (IOException e) { throw new OXFException(e); } } } // Create file if it doesn't exist (necessary when the file size is 0) final File storeLocation = ((DiskFileItem) fileItem).getStoreLocation(); try { storeLocation.createNewFile(); } catch (IOException e) { throw new OXFException(e); } return fileItem; }
/** * 接受并保存以base64格式上传的文件 * * @param fieldName */ public void uploadBase64(String fieldName) { String savePath = this.getFolder(this.savePath); String base64Data = this.request.getParameter(fieldName); this.fileName = this.getName("test.png"); this.url = savePath + "/" + this.fileName; try { File outFile = new File(this.getPhysicalPath(this.url)); OutputStream ro = new FileOutputStream(outFile); byte[] b = Base64.decodeBase64(base64Data); ro.write(b); ro.flush(); ro.close(); this.state = this.errorInfo.get("SUCCESS"); } catch (Exception e) { this.state = this.errorInfo.get("IO"); } }
public static void copyStream(InputStream is, OutputStream os) throws IOException { int count; byte[] buffer = new byte[1024]; while ((count = is.read(buffer)) > 0) os.write(buffer, 0, count); }