Upload the specified file to the server

Source: Internet
Author: 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: 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.