flex 上传文件java端接收

// java 端
String tempPath = WebFilePath.getUploadTempUrl(getRequest());
		String fileName_prefix = StringUUID.stringUUID() + "_"
				+ System.currentTimeMillis();
//		String uploadPath = tempPath;

		MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) ServletActionContext
				.getRequest();

		// Bind allowed Files
		Enumeration<String> fileParameterNames = multiWrapper
				.getFileParameterNames();
		while (fileParameterNames != null
				&& fileParameterNames.hasMoreElements()) {
			// get the value of this input tag
			String inputName = (String) fileParameterNames.nextElement();
			// get the content type
			String[] contentType = multiWrapper.getContentTypes(inputName);
			if (!isNonEmpty(contentType)) {
				continue;
			}
			// get the name of the file from the input tag
			String[] fileNames = multiWrapper.getFileNames(inputName);
			if (!isNonEmpty(fileNames)) {
				continue;
			}
			// Get a File object for the uploaded File
			File[] files = multiWrapper.getFiles(inputName);
			if (files == null) {
				continue;
			}
			for (int i = 0; i < files.length; i++) {
				String fileName = fileNames[i];
				if (StringUtil.isNullOrEmpty(fileName)) {
					continue;
				}
				String suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
				CommonFileUtil.writeFile(files[i], tempPath, fileName_prefix+suffix);
			}
		}


 

private static boolean isNonEmpty(Object[] objArray) {
		boolean result = false;
		for (int index = 0; index < objArray.length && !result; index++) {
			if (objArray[index] != null) {
				result = true;
			}
		}
		return result;
	}

/**
	 * 获得存放上传文件的地址
	 * @param request
	 * @return
	 */
	public static String getUploadWebUrl(HttpServletRequest request) {
		return request.getSession().getServletContext().getRealPath("") + File.separator + UPLOAD + File.separator;
	}


 

/**
	 * 写文件
	 * @param file
	 * @param filePath
	 * @param fileName
	 * @return
	 */
	public static boolean writeFile(File file, String filePath,String fileName) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			if(StringUtil.isNullOrEmpty(filePath) || StringUtil.isNullOrEmpty(fileName)){
				ExceptionUtil.warn("写文件失败,文件位置:"+filePath+",文件名:"+fileName, SystemType.系统);
				return false;
			}
			File fileFolder = new File(filePath);
			if(!fileFolder.exists()){
				if(!fileFolder.mkdirs()){
					ExceptionUtil.warn("写文件时,文件夹创建失败!文件位置:"+filePath+",文件名:"+fileName, SystemType.系统);
					return false;
				}
				
			}
			fis = new FileInputStream(file);
			fos = new FileOutputStream(fileFolder.getAbsoluteFile()+File.separator+fileName);
			byte[] b = new byte[1024];
			int i = 0;
			while ((i = fis.read(b)) > 0) {
				fos.write(b, 0, i);
			}
			return true;
		} catch (Exception ex) {
			ExceptionUtil.error(ex, null, SystemType.系统);

		} finally {
			if (null != fos) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != fis) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	}


by : 如流,新一代智能工作平台

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值