`
huangyongxing310
  • 浏览: 482048 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

smb上传下载文件

 
阅读更多
/***
	 * 从局域网共享文件获取文件输入流
	 * @param remotePathAndFileName:共享的相对路径+文件名(含后缀),如:/qualityNotice/我们的祖国是花园.txt
	 * @return
	 */
	public static InputStream smbGetFile(String remotePathAndFileName){
		String fullPath = "";
		String lastChar = remotePathAndFileName.substring(0,1);
		SmbFileInputStream in = null;
		String ip = CommonManage.smbgetConnectIp();
		String user = CommonManage.smbgetConnectUser();
		String pass = CommonManage.smbgetConnectPass();
		String targetPath = "smb://" + user + ":" + pass + "@" + ip + "/htwyRes";
		if (lastChar.equals("/") || lastChar.equals("\\"))
			fullPath = targetPath + remotePathAndFileName.trim();
		else
			fullPath = targetPath + "/" + remotePathAndFileName.trim();
		SmbFile file = null;
		System.out.println("下载路径:"+fullPath);
		try {
			file = new SmbFile(fullPath);
			System.out.println("下载文件:"+file);
			in = new SmbFileInputStream(file);
			System.out.println("输出流:"+in);
		} catch (Exception e) {
			try {
				if (in==null){
					fullPath = targetPath + "/noPic.jpg";
					file = new SmbFile(fullPath);
					in = new SmbFileInputStream(file);
				}
			} catch (Exception e2) {
				
			}
		}
		return in;
	}








/***
	 * 从局域网共享文件下载文件到指定目录
	 * @param remotePathAndFileName		共享的相对路径+文件名(含后缀),如:/qualityNotice/我们的祖国是花园.txt
	 * @param localPath					要存放的本地目录
	 * @param realFileName				下载后的文件名(含后缀),如果为空则按共享文件夹上的文件名(uuid),如果指定了按指定的,例如:我们的祖国是花园.txt
	 * @return
	 */
	public static fileUploadReturn smbDownloadFile(String remotePathAndFileName,String localPath,String realFileName){
		InputStream in = null;
		OutputStream out = null;
		fileUploadReturn fur = new fileUploadReturn();
		String ip = CommonManage.smbgetConnectIp();
		String user = CommonManage.smbgetConnectUser();
		String pass = CommonManage.smbgetConnectPass();
		try{
			File localFile = null;
			String targetFullPath = "";
			String oneChar = remotePathAndFileName.substring(0,1);
			String targetPath = "smb://" + user + ":" + pass + "@" + ip + "/htwyRes";
			if (oneChar.equals("/") || oneChar.equals("\\"))
				targetFullPath = targetPath + remotePathAndFileName.trim();
			else
				targetFullPath = targetPath + "/" + remotePathAndFileName.trim();

			SmbFile smbFile = new SmbFile(targetFullPath);
			if (smbFile==null){
				targetFullPath = targetPath + "/noPic.jpg";
				smbFile = new SmbFile(targetFullPath);
			}
			oneChar = localPath.substring(localPath.length()-1);
			if (!oneChar.equals("/") && !oneChar.equals("\\"))
				localPath = localPath + File.separator;

			File existsTest = new File(localPath);
			if (!existsTest.exists())
				existsTest.mkdirs();
			
			String fileName = smbFile.getName();
			if (realFileName.equals("") || realFileName==null){
				localFile = new File(localPath + fileName);
				fur.setSysFileName(fileName);
			} else {
				localFile = new File(localPath + realFileName);
				fur.setSysFileName(realFileName);
			}

			in = new SmbFileInputStream(smbFile);
			out = new FileOutputStream(localFile);
			byte[] buffer = new byte[1024];
			int length = 0;
			while ((length = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}

			fur.setSucess(true);
			fur.setMsg("下载成功。");
		} catch (Exception e) {
			fur.setSucess(false);
			fur.setMsg("下载失败。");
			e.printStackTrace();
			return fur;
		} finally {
			try {
				if (out!=null)
					out.close();
				if (in!=null)
					in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return fur;
	}

	/***
	 * 判断局域网共享文件下的文件是否存在
	 * @param remotePathAndFile		共享电脑路径加文件 如/qualityNotice/a213dadfdc35z432d2yu9.txt
	 * @return
	 */
	public static boolean smbCheckFileExists(String remotePathAndFile){
		boolean isExists = true;
		String ip = CommonManage.smbgetConnectIp();
		String user = CommonManage.smbgetConnectUser();
		String pass = CommonManage.smbgetConnectPass();
		try {
			String targetFullPath = "";
			String oneChar = remotePathAndFile.substring(0,1);
			String targetPath = "smb://" + user + ":" + pass + "@" + ip + "/htwyRes";
			if (oneChar.equals("/") || oneChar.equals("\\"))
				targetFullPath = targetPath + remotePathAndFile.trim();
			else
				targetFullPath = targetPath + "/" + remotePathAndFile.trim();
			
			SmbFile checkFile = new SmbFile(targetFullPath);
			if (checkFile.exists() && checkFile.isFile()){
				
			} else
				isExists = false;
		} catch (Exception e) {
			isExists = false;
			e.printStackTrace();
		}
		return isExists;
	}







/***
	 * 把本地磁盘中的文件上传到局域网共享文件下
	 * @param remotePath			共享电脑路径 如:/qualityNotice
	 * @param localPathAndFileName	完整的本地的文件路径,如 C:/htwy/我们的祖国是花园.txt
	 */
	public static fileUploadReturn smbUploadFile(String remotePath, String localPathAndFileName){
		InputStream in = null;
		OutputStream out = null;
		fileUploadReturn fur = new fileUploadReturn();
		String ip = CommonManage.smbgetConnectIp();
		String user = CommonManage.smbgetConnectUser();
		String pass = CommonManage.smbgetConnectPass();
		try {
			File localFile = new File(localPathAndFileName);
			String targetFullPath = "";
			String oneChar = remotePath.substring(0,1);
			String targetPath = "smb://" + user + ":" + pass + "@" + ip + "/htwyRes";
			if (oneChar.equals("/") || oneChar.equals("\\"))
				targetFullPath = targetPath + remotePath.trim();
			else
				targetFullPath = targetPath + "/" + remotePath.trim();

			SmbFile existsTest = new SmbFile(targetFullPath);
			if (!existsTest.exists())
				existsTest.mkdirs();
			
			String fileExt = localPathAndFileName.substring(localPathAndFileName.lastIndexOf('.') + 1);
			if (fileExt.equals("") || fileExt==null){
				fur.setSucess(false);
				fur.setMsg("文件名应包含后缀名。");
			} else {
				String sysFileName = CommonManage.getUuid() + "." + fileExt;
				SmbFile remoteFile = null;
				oneChar = targetFullPath.substring(targetFullPath.length()-1);
				if (oneChar.equals("/") || oneChar.equals("\\"))
					remoteFile = new SmbFile(targetFullPath + sysFileName);
				else
					remoteFile = new SmbFile(targetFullPath + "/" + sysFileName);

				in = new FileInputStream(localFile);
				out = new SmbFileOutputStream(remoteFile);
				byte[] buffer = new byte[1024];
				int length = 0;
				while ((length = in.read(buffer)) > 0) {
					out.write(buffer, 0, length);
				}

				fur.setSucess(true);
				fur.setSysFileName(sysFileName);
				fur.setMsg("上传成功。");
			}
		} catch (Exception e) {
			fur.setSucess(false);
			fur.setMsg("上传失败。");
			e.printStackTrace();
		} finally {
			try {
				out.close();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return fur;
	}

	/***
	 * 把本地磁盘中的文件上传到局域网共享文件下
	 * @param remotePath	共享电脑路径 如:/qualityNotice
	 * @param inputStream	文件的InputStream形式
	 * @param fileName		本地的文件名(含后缀名),如:我们的祖国是花园.txt
	 * @return
	 */
	public static fileUploadReturn smbUploadFile(String remotePath, InputStream inputStream, String fileName){
		InputStream in = null;
		OutputStream out = null;
		fileUploadReturn fur = new fileUploadReturn();
		String ip = CommonManage.smbgetConnectIp();
		String user = CommonManage.smbgetConnectUser();
		String pass = CommonManage.smbgetConnectPass();
		try {
			String targetFullPath = "";
			String oneChar = remotePath.substring(0,1);
			String targetPath = "smb://" + user + ":" + pass + "@" + ip + "/htwyRes";
			if (oneChar.equals("/") || oneChar.equals("\\"))
				targetFullPath = targetPath + remotePath.trim();
			else
				targetFullPath = targetPath + "/" + remotePath.trim();

			SmbFile existsTest = new SmbFile(targetFullPath);
			if (!existsTest.exists())
				existsTest.mkdirs();
			
			String fileExt = fileName.substring(fileName.lastIndexOf('.') + 1);
			if (fileExt.equals("") || fileExt==null){
				fur.setSucess(false);
				fur.setMsg("文件名应包含后缀名。");
			} else {
				String sysFileName = CommonManage.getUuid() + "." + fileExt;
				SmbFile remoteFile = null;
				oneChar = targetFullPath.substring(targetFullPath.length()-1);
				if (oneChar.equals("/") || oneChar.equals("\\"))
					remoteFile = new SmbFile(targetFullPath + sysFileName);
				else
					remoteFile = new SmbFile(targetFullPath + "/" + sysFileName);

				in = inputStream;
				out = new SmbFileOutputStream(remoteFile);
				byte[] buffer = new byte[1024];
				int length = 0;
				while ((length = in.read(buffer)) > 0) {
					out.write(buffer, 0, length);
				}

				fur.setSucess(true);
				fur.setSysFileName(sysFileName);
				fur.setMsg("上传成功。");
			}
		} catch (Exception e) {
			fur.setSucess(false);
			fur.setMsg("上传失败。");
			e.printStackTrace();
		} finally {
			try {
				out.close();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return fur;
	}

	/***
	 * 把本地磁盘中的文件上传到局域网共享文件下并覆盖文件
	 * @param remotePath	共享电脑路径 如:/qualityNotice
	 * @param inputStream	文件的InputStream形式
	 * @param fileName		覆盖的文件名(含后缀名),如:我们的祖国是花园.txt
	 * @return
	 */
	public static fileUploadReturn smbUploadFileEdit(String remotePath, InputStream inputStream, String fileName){
		InputStream in = null;
		OutputStream out = null;
		fileUploadReturn fur = new fileUploadReturn();
		String ip = CommonManage.smbgetConnectIp();
		String user = CommonManage.smbgetConnectUser();
		String pass = CommonManage.smbgetConnectPass();
		try {
			String targetFullPath = "";
			String oneChar = remotePath.substring(0,1);
			String targetPath = "smb://" + user + ":" + pass + "@" + ip + "/htwyRes";
			if (oneChar.equals("/") || oneChar.equals("\\"))
				targetFullPath = targetPath + remotePath.trim();
			else
				targetFullPath = targetPath + "/" + remotePath.trim();

			SmbFile existsTest = new SmbFile(targetFullPath);
			if (!existsTest.exists())
				existsTest.mkdirs();
			
			String fileExt = fileName.substring(fileName.lastIndexOf('.') + 1);
			if (fileExt.equals("") || fileExt==null){
				fur.setSucess(false);
				fur.setMsg("文件名应包含后缀名。");
			} else {
				String sysFileName = fileName;
				SmbFile remoteFile = null;
				oneChar = targetFullPath.substring(targetFullPath.length()-1);
				if (oneChar.equals("/") || oneChar.equals("\\"))
					remoteFile = new SmbFile(targetFullPath + sysFileName);
				else
					remoteFile = new SmbFile(targetFullPath + "/" + sysFileName);

				in = inputStream;
				out = new SmbFileOutputStream(remoteFile);
				byte[] buffer = new byte[1024];
				int length = 0;
				while ((length = in.read(buffer)) > 0) {
					out.write(buffer, 0, length);
				}

				fur.setSucess(true);
				fur.setSysFileName(sysFileName);
				fur.setMsg("上传成功。");
			}
		} catch (Exception e) {
			fur.setSucess(false);
			fur.setMsg("上传失败。");
			e.printStackTrace();
		} finally {
			try {
				out.close();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return fur;
	}





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics