Java Code Examples for org.apache.struts.upload.FormFile#getInputStream()

The following examples show how to use org.apache.struts.upload.FormFile#getInputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyTools.java    From Films with Apache License 2.0 8 votes vote down vote up
public static String uploadUserPhoto(HttpServletRequest request,FormFile ff,String id){

 String newPhotoName="";
 try{
	//���Ǹ�ÿ���û������Լ����ļ���.
	String filePath=request.getSession().getServletContext().getRealPath("/");
	//filePath���ǵ�ǰ���webӦ���Ǿ��·�� F:\apache-tomcat-6.0.20\webapps\xiaoneinew
	//E:\Workspaces\MyEclipse 10\.metadata\.me_tcat\webapps\Films\
	InputStream stream = ff.getInputStream();// ���ļ�����
	String oldPhotoName=ff.getFileName();
	newPhotoName=id+oldPhotoName.substring(oldPhotoName.indexOf("."), oldPhotoName.length());
	String newFullNewPath=filePath+File.separator+"upload"+File.separator+ "Gravator"+File.separator + id +File.separator;
	//�ж�newFullNewPath�ļ����Ƿ����
	File f=new File(newFullNewPath);
	if(!f.isDirectory()){
		//�����ļ���,��������
		f.mkdirs();
		
	}
	//���ϴ���ͷ�������޳ɳ� ���.��׺
	
	OutputStream bos = new FileOutputStream(newFullNewPath+ newPhotoName);
	int len = 0;
	byte[] buffer = new byte[8192];
	while ((len = stream.read(buffer, 0, 8192)) != -1) {
		bos.write(buffer, 0, len);// ���ļ�д�������
	}
	bos.close();
	stream.close();

} catch (Exception e) {
	e.printStackTrace();
}
return newPhotoName;
}
 
Example 2
Source File: ImportBaseFileAction.java    From openemm with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Stores uploaded csv file to temporary directory and the location is
 * store to session. So the file can be used later.
 *
 * @param request request
 * @param csvFile uploaded csv file
 * @return errors that happened
 * @throws IOException 
 * @throws FileNotFoundException 
 */
private ActionErrors storeCsvFile(HttpServletRequest request, FormFile csvFile) throws Exception {
    ActionErrors errors = new ActionErrors();
    HttpSession session = request.getSession();
    String savePath = generateSavePath(session);
    File file = new File(savePath);
    try (InputStream inputStream = csvFile.getInputStream()) {
     try (FileOutputStream outputStream = new FileOutputStream(file, false)) {
         IOUtils.copy(inputStream, outputStream);
         removeStoredCsvFile(request);
         session.setAttribute(CSV_FILE_PATH_KEY, savePath);
         session.setAttribute(CSV_ORIGINAL_FILE_NAME_KEY, csvFile.getFileName());
         fileUploadPerformed = true;
     } catch (IOException e) {
         errors.add("csvFile", new ActionMessage("error.import.cannotOpenFile", e.getMessage()));
         return errors;
     } finally {
     	IOUtils.closeQuietly(inputStream);
     }
    }
    return errors;
}
 
Example 3
Source File: AjaxAttachFileAction.java    From ezScrum with GNU General Public License v2.0 6 votes vote down vote up
private File convertToFile(FormFile formFile) throws IOException {
	String tempUploadFolder = "upload_tmp";
	
	File folder = new File(tempUploadFolder);
	folder.mkdirs();
	File file = new File(tempUploadFolder + File.separator + formFile.getFileName());
	OutputStream os = new FileOutputStream(file);
	InputStream is = new BufferedInputStream(formFile.getInputStream());
	int count;
	byte[] buffer = new byte[4096];
	while ((count = is.read(buffer)) > -1) {
		os.write(buffer, 0, count);
	}
	
	os.close();
	is.close();
	return file;
}
 
Example 4
Source File: ComMailingComponentsServiceImpl.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public UploadStatistics uploadZipArchive(Mailing mailing, FormFile zipFile) throws Exception {
	UploadStatisticsImpl statistics = new UploadStatisticsImpl();

	try (ZipInputStream zipStream = new ZipInputStream(zipFile.getInputStream())) {
		for (ZipEntry entry = zipStream.getNextEntry(); entry != null; entry = zipStream.getNextEntry()) {
			final String path = entry.getName();
			final String name = FilenameUtils.getName(path);

			if (!entry.isDirectory() && ImageUtils.isValidImageFileExtension(FilenameUtils.getExtension(name))) {
				statistics.newFound();
				logger.info("uploadZipArchive(): found image file '" + path + "' in ZIP stream");
				byte[] content = StreamHelper.streamToByteArray(zipStream);
				if (ImageUtils.isValidImage(content)) {
					addComponent(mailing, content, name);
					statistics.newStored();
				}
			}

			zipStream.closeEntry();
		}
	} catch (IOException e) {
		logger.error("uploadZipArchive()", e);
		throw e;
	}

	return statistics;
}
 
Example 5
Source File: MyTools.java    From Films with Apache License 2.0 5 votes vote down vote up
public static String uploadFilmPhoto(HttpServletRequest request,FormFile ff,String id){

 String newPhotoName="";
 try{
	//���Ǹ�ÿ���û������Լ����ļ���.
	String filePath=request.getSession().getServletContext().getRealPath("/");
	//filePath���ǵ�ǰ���webӦ���Ǿ��·�� F:\apache-tomcat-6.0.20\webapps\xiaoneinew
	//E:\Workspaces\MyEclipse 10\.metadata\.me_tcat\webapps\Films\
	InputStream stream = ff.getInputStream();// ���ļ�����
	String oldPhotoName=ff.getFileName();
	newPhotoName=id+oldPhotoName.substring(oldPhotoName.indexOf("."), oldPhotoName.length());
	String newFullNewPath=filePath+File.separator+"upload"+File.separator+"movie"+File.separator;
	//�ж�newFullNewPath�ļ����Ƿ����
	File f=new File(newFullNewPath);
	if(!f.isDirectory()){
		//�����ļ���,��������
		f.mkdirs();
		
	}
	//���ϴ���ͷ�������޳ɳ� ���.��׺
	
	OutputStream bos = new FileOutputStream(newFullNewPath+ newPhotoName);
	int len = 0;
	byte[] buffer = new byte[8192];
	while ((len = stream.read(buffer, 0, 8192)) != -1) {
		bos.write(buffer, 0, len);// ���ļ�д�������
	}
	bos.close();
	stream.close();

} catch (Exception e) {
	e.printStackTrace();
}
return newPhotoName;
}
 
Example 6
Source File: KualiBatchInputFileAction.java    From kfs with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Sends the uploaded file contents, requested file name, and batch type to the BatchInputTypeService for storage. If errors
 * were encountered, messages will be in GlobalVariables.errorMap, which is checked and set for display by the request
 * processor.
 */
public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    BatchUpload batchUpload = ((KualiBatchInputFileForm) form).getBatchUpload();
    BatchInputFileType batchType = retrieveBatchInputFileTypeImpl(batchUpload.getBatchInputTypeName());

    BatchInputFileService batchInputFileService = SpringContext.getBean(BatchInputFileService.class);
    FormFile uploadedFile = ((KualiBatchInputFileForm) form).getUploadFile();

    if (uploadedFile == null || uploadedFile.getInputStream() == null || uploadedFile.getInputStream().available() == 0) {
        GlobalVariables.getMessageMap().putError(KFSConstants.GLOBAL_ERRORS, KFSKeyConstants.ERROR_BATCH_UPLOAD_NO_FILE_SELECTED_SAVE, new String[] {});
        return mapping.findForward(KFSConstants.MAPPING_BASIC);
    }

    if (!batchInputFileService.isFileUserIdentifierProperlyFormatted(batchUpload.getFileUserIdentifer())) {
        GlobalVariables.getMessageMap().putError(KFSConstants.GLOBAL_ERRORS, KFSKeyConstants.ERROR_BATCH_UPLOAD_FILE_USER_IDENTIFIER_BAD_FORMAT, new String[] {});
        return mapping.findForward(KFSConstants.MAPPING_BASIC);
    }

    InputStream fileContents = ((KualiBatchInputFileForm) form).getUploadFile().getInputStream();
    byte[] fileByteContent = IOUtils.toByteArray(fileContents);
    
    Object parsedObject = null;
    try {
        parsedObject = batchInputFileService.parse(batchType, fileByteContent);
    }
    catch (ParseException e) {
        LOG.error("errors parsing xml " + e.getMessage(), e);
        GlobalVariables.getMessageMap().putError(KFSConstants.GLOBAL_ERRORS, KFSKeyConstants.ERROR_BATCH_UPLOAD_PARSING_XML, new String[] { e.getMessage() });
    }

    if (parsedObject != null && GlobalVariables.getMessageMap().hasNoErrors()) {
        boolean validateSuccessful = batchInputFileService.validate(batchType, parsedObject);

        if (validateSuccessful && GlobalVariables.getMessageMap().hasNoErrors()) {
            try {
                InputStream saveStream = new ByteArrayInputStream(fileByteContent);

                String savedFileName = batchInputFileService.save(GlobalVariables.getUserSession().getPerson(), batchType, batchUpload.getFileUserIdentifer(), saveStream, parsedObject);
                KNSGlobalVariables.getMessageList().add(KFSKeyConstants.MESSAGE_BATCH_UPLOAD_SAVE_SUCCESSFUL);
            }
            catch (FileStorageException e1) {
                LOG.error("errors saving xml " + e1.getMessage(), e1);
                GlobalVariables.getMessageMap().putError(KFSConstants.GLOBAL_ERRORS, KFSKeyConstants.ERROR_BATCH_UPLOAD_SAVE, new String[] { e1.getMessage() });
            }
        }
    }

    return mapping.findForward(KFSConstants.MAPPING_BASIC);
}