Created
August 31, 2014 04:38
-
-
Save Sentient07/e6280795f74efa3a18da to your computer and use it in GitHub Desktop.
Simple FTP server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package ftpjava; | |
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/** | |
* | |
* @author ramana | |
*/ | |
public class Server implements Runnable { | |
public static void main(String [] args) throws IOException{ | |
new Thread(new Server()).start(); | |
} | |
@Override | |
public void run() { | |
try { | |
ServerSocket servSocket = new ServerSocket(8880); | |
Socket fSock = servSocket.accept(); | |
while(true){ | |
try { | |
String message; | |
while(((message = new BufferedReader(new | |
InputStreamReader(fSock.getInputStream())) | |
.readLine()) != null )){ | |
boolean operator = messageParser(message); | |
OutputStream ostream = fSock.getOutputStream(); | |
if(operator == true){ | |
sendFile(ostream, message); | |
} | |
} | |
} catch (IOException ex) { | |
Logger.getLogger(Server.class.getName()). | |
log(Level.SEVERE, null, ex); | |
} | |
} | |
} catch (IOException ex) { | |
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
private boolean messageParser(String message){ | |
boolean return_value; | |
final String pattern = "^!file:"; | |
Pattern ptr = Pattern.compile(pattern); | |
Matcher match = ptr.matcher(message); | |
if(!(match.find())){ | |
return_value = true; | |
}else { | |
return_value = false; | |
} | |
return return_value; | |
} | |
//StoresFile on the server | |
private void StoresFile(InputStream is) throws IOException{ | |
int max_size = 99999; | |
byte[] byarr = new byte[max_size]; | |
BufferedOutputStream bostream = new BufferedOutputStream | |
(new FileOutputStream("received")); | |
int bysize, currentSize = 0; | |
bysize = is.read(byarr, 0, byarr.length); | |
currentSize = bysize; | |
do { | |
bysize = is.read(byarr, currentSize, (bysize-currentSize)); | |
if(bysize > 0){ | |
currentSize += bysize; | |
} | |
}while(bysize>-1); | |
bostream.write(byarr, 0, bysize); | |
bostream.flush(); | |
bostream.close(); | |
} | |
private void sendFile(OutputStream os, String PathToFile) throws IOException{ | |
File file_object = new File(PathToFile); | |
BufferedInputStream bistream = new BufferedInputStream | |
(new FileInputStream(file_object)); | |
byte[] bytearray = new byte[(int) file_object.length()]; | |
bistream.read(bytearray, 0, (int) file_object.length()); | |
os.write(bytearray); | |
os.flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment