往伺服器上傳指定檔案

來源:互聯網
上載者:User

標籤:


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;

public class PublicFileUtil
{
private static Logger logger = Logger.getLogger(FTPclientUtil.class);

/**
* Description: 往伺服器上傳指定檔案 。
* 1、路徑不存在,可以建立路徑
* 2、可以上傳指定格式的檔案
*
* @param remotePath
* util.properties配製的路徑key
* eg:resource_root.courseware,前面不要系統帶環境標示
* @param fileName
* 上傳到FTP伺服器上的檔案名稱
*
* @param finput
* 本地路徑 輸入資料流
*
* @param isDir
* false 伺服器路徑不存在,不建立,直接返迴路徑不存在(路徑有許可權控制)
*
* @return 成功返回success
* @throws IOException
*/
public static String webUploadFile(String remotePath, String fileName,
InputStream finput, boolean isDir) {
String result = "uploadError";

String firstFileName = fileName.substring(0,fileName.lastIndexOf("."));
String endFileName = fileName.substring(fileName.lastIndexOf("."),fileName.length()).toLowerCase();
firstFileName=firstFileName+"^"+System.currentTimeMillis();
fileName = firstFileName+endFileName;
if (CheckNull.isNull(finput)) {
// 上傳的檔案不存在
return "undefindFile";
}

// 判斷環境
String fileSavePath =PublicUtil.getSystemRemotePath(remotePath);

// 如果檔案夾不存在就自動建立一個檔案夾
File fileTemp = new File(fileSavePath);
if (!(fileTemp.exists()) && !(fileTemp.isDirectory()))
{
if(isDir)
{
fileTemp.mkdirs();
}
else
{
//直接返回遠程路徑不存在
return "undefindRemort";
}
}

FileOutputStream fos = null;
boolean falg = true;

byte[] buffer = new byte[1024];
int len = 0;
try
{
fos = new FileOutputStream(fileSavePath + File.separator + fileName);
while ((len = finput.read(buffer)) > 0)
{
fos.write(buffer, 0, len);
}
fos.flush();
}
catch (IOException e)
{
logger.error("---上傳檔案失敗!---", e);
falg = false;
}

// 關閉流
if (null != finput)
{
try
{
finput.close();
}
catch (IOException e)
{
logger.error("---上傳檔案關閉流失敗!---", e);
falg = false;
}
}
if (null != fos)
{
try
{
fos.close();
}
catch (IOException e)
{
logger.error("---上傳檔案關閉流失敗!---", e);
falg = false;
}
}

if (falg)
{
result = fileName;
}
return result;
}

/**
* 關閉串連
*/
public static void closeConnect(FTPClient ftpClient, OutputStream os,
FileInputStream fis,InputStream ins) {
try {
if (null != ftpClient) {
ftpClient.logout();
ftpClient.disconnect();
}

if (null != os) {
os.flush();
os.close();
}

if (null != fis) {
fis.close();
}

if (null != ins) {
ins.close();
}

} catch (Exception e) {
e.printStackTrace();
}
}

}

往伺服器上傳指定檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.