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: Uploads the specified file to the server.
* 1, path does not exist, you can create a path
* 2, you can upload a file in the specified format
*
* @param remotepath
* util.properties The path of the configuration key
* Eg:resource_r Oot.courseware, no front, no system.
* @param filename
* Upload file name on FTP server
*
* @param finput
* Local Path input stream
*
* @param isdir
* False server path does not exist, not created, direct return path does not exist (Path has permission control)
*
* @return successfully returned 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)) {
//uploaded file does not exist
return "Undefindfile";
}
Judging the environment
String Filesavepath =publicutil.getsystemremotepath (RemotePath);
Create a folder automatically if it doesn't exist
File Filetemp = new file (Filesavepath);
if (! ( Filetemp.exists ()) &&! (Filetemp.isdirectory ()))
{
if (Isdir)
{
Filetemp.mkdirs ();
}
Else
{
Direct return remote path does not exist
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 ("---upload file failed! ---", e);
Falg = false;
}
Close the stream
if (null! = Finput)
{
Try
{
Finput.close ();
}
catch (IOException E)
{
Logger.error ("---upload file closed stream failed! ---", e);
Falg = false;
}
}
if (null! = FOS)
{
Try
{
Fos.close ();
}
catch (IOException E)
{
Logger.error ("---upload file closed stream failed! ---", e);
Falg = false;
}
}
if (FALG)
{
result = FileName;
}
return result;
}
/**
* Close Connection
*/
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 ();
}
}
}
Upload the specified file to the server