New file is getting created during FTP download - java

I'm trying to download a file from server using FTP, the Java code works if the file is available in remote server but if the specific file is not available in the remote server a new file is getting created with same file name in local. How can I avoid this?
and I'm trying to check the properties such as last modified time, file created time etc.., of the specific file before download, I used MLST but getting type casting issues..!!
import java.io.BufferedOutputStream;
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.FTP;
import org.apache.commons.net.ftp.FTPClient;
public class FTPDownloadFileDemo {
public static void main(String[] args) {
String server = "www.myserver.com";
int port = 21;
String user = "user";
String pass = "pass";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// APPROACH #1: using retrieveFile(String, OutputStream)
String remoteFile1 = "/test/video.mp4";
File downloadFile1 = new File("D:/Downloads/video.mp4");
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
}
outputStream2.close();
inputStream.close();
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

The retrieveFile() method always writes a local file, whether or not the remote file exists. Instead, you can use retrieveFileStream() and check the reply code.
A handy list of FTP reply codes is available from Wikipedia. If 550 is received, it means the file does not exist.
Finally, you need to use completePendingCommand() to complete the transaction and a FileOutputStream to write the file.
InputStream inputStream = ftpClient.retrieveFileStream(remoteFile1);
int returnCode = ftpClient.getReplyCode();
if (inputStream == null || returnCode == 550) {
System.out.println("Remote file does not exist");
} else {
ftpClient.completePendingCommand();
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
OutputStream outputStream = new FileOutputStream(downloadFile1);
outputStream.write(buffer);
outputStream.close();
}

Your problem is that your Outputstream automatically creates the File, even if the stream is empty.
I would recommend you check first if the file exists on the server and based on that you don't even create the outputStream:
boolean checkFileExists(String filePath) throws IOException {
InputStream inputStream = ftpClient.retrieveFileStream(remoteFile1);
returnCode = ftpClient.getReplyCode();
return inputStream == null || returnCode == 550;
}

Related

Is it possible to retrieve a file from a remote path using java?

I've been tasked with a project to automate a process in which we extract a file from a WinSCP client daily. So far I've been able to automate the login and setup a recurring schedule for the code to run; however it seems I've hit a bump in the road. When I attempt to locate a file for retrieval nothing happens. This is because the file I wish to access is through a remote directory. I'm almost positive that the code I've written is bug free. I am just unsure if specify a certain path which java can locate the file. I have no idea how to tell the java code where to extract this file from. Any thoughts?
You can try and use the code below:
More details can be found here.
import java.io.BufferedOutputStream;
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.FTP;
import org.apache.commons.net.ftp.FTPClient;
/**
* A program demonstrates how to upload files from local computer to a remote
* FTP server using Apache Commons Net API.
* #author www.codejava.net
*/
public class FTPDownloadFileDemo {
public static void main(String[] args) {
String server = "www.myserver.com";
int port = 21;
String user = "user";
String pass = "pass";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// APPROACH #1: using retrieveFile(String, OutputStream)
String remoteFile1 = "/test/video.mp4";
File downloadFile1 = new File("D:/Downloads/video.mp4");
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
}
// APPROACH #2: using InputStream retrieveFileStream(String)
String remoteFile2 = "/test/song.mp3";
File downloadFile2 = new File("D:/Downloads/song.mp3");
OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2));
InputStream inputStream = ftpClient.retrieveFileStream(remoteFile2);
byte[] bytesArray = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(bytesArray)) != -1) {
outputStream2.write(bytesArray, 0, bytesRead);
}
success = ftpClient.completePendingCommand();
if (success) {
System.out.println("File #2 has been downloaded successfully.");
}
outputStream2.close();
inputStream.close();
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

How to delete directory using java after uploading files to Remote Server?

My problem is need to transfer files from one remote server to another remote server (may be FTP/SFTP) but there is no direct method to transfer files from one remote server to another.
That's why I am downloading files from server to local temp.
After uploading to local to another server. After uploading I need to remove local temp folder but the files and the folder is not deleted.
Can you please help us in this regard?
My code is
package FTPTransfer;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.File;
import java.util.Calendar;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import com.jcraft.jsch.*;
public class FtpToSftp
{
JSch sftp=null;
ChannelSftp channelSftp=null;
Channel channel=null;
FTPClient ftp = null;
Session session=null;
String SFTP_ROOT="/Mahesh/";
String FTP_ROOT="/Mahesh/";
String Local_Dir="./Temp/";
int count=0;
public void ftpconnect(String host, String user, String pwd) throws Exception{
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.connect(host);
if(ftp.isConnected())
System.out.println("FTP Connected");
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new Exception("Exception in connecting to FTP Server");
}
ftp.login(user, pwd);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
}
public void sftpconnect(String host, String user, String pwd) throws Exception{
sftp=new JSch();
session=sftp.getSession(user,host,22);
session.setPassword(pwd);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
if(session.isConnected())
System.out.println("SFTP Session Connected");
channel = session.openChannel("sftp");
channel.connect();
if(channel.isConnected())
System.out.println("SFTP Channel Connected");
channelSftp=(ChannelSftp)channel;
}
public void downloadFromFTP()throws Exception {
File f=new File(Local_Dir);
if(!f.exists())
f.mkdir();
FTPFile[] files = ftp.listFiles(FTP_ROOT);
count=0;
OutputStream outputStream=null;
for (FTPFile fname : files) {
if (fname.getType() == FTPFile.FILE_TYPE) {
System.out.println(fname.getName());
File downloadFile = new File(Local_Dir+ fname.getName());
outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
boolean success = ftp.retrieveFile(FTP_ROOT+fname.getName(), outputStream);
if(success)
count++;
else
downloadFile.delete();
}
}
if(count==files.length)
System.out.println("Files Downloaded Successfully");
System.out.println("count:"+count+"files length:"+files.length);
outputStream.close();
}
public void uploadToSFTP() throws Exception{
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH)+1;//0 based
String foldername=month+""+year+"/";
String fullDirPath=SFTP_ROOT+foldername;
SftpATTRS attrs=null;
try{
attrs=channelSftp.lstat(fullDirPath);
}
catch(Exception e){
}
if(attrs==null)
{
channelSftp.mkdir(fullDirPath);
channelSftp.cd(fullDirPath);
}
count=0;
File f1 = new File(Local_Dir);
File list[] = f1.listFiles();
for(File fname : list) {
System.out.println(fname);
channelSftp.put(fname+"", fullDirPath+fname.getName(), ChannelSftp.OVERWRITE);
}
if(count==f1.length())
System.out.println("Files Uploaded Successfully");
}
public FtpToSftp() throws Exception{
System.out.println("Connecting to FTP");
ftpconnect("10.219.28.110", "webteam", "web$123");
System.out.println("Connecting to SFTP");
sftpconnect("10.219.29.61","root" , "leo$123");
downloadFromFTP();
if(ftp.logout()){
ftp.disconnect();
System.out.println("FTP connection closed");
}
uploadToSFTP();
channelSftp.disconnect();
}
public static final void main(String[] args)
{
try{
FtpToSftp fs=new FtpToSftp();
File file=new File(fs.Local_Dir);
if(file.isDirectory())
{
File[] files = file.listFiles();
for (File f : files)
{
String fname=f.getName();
boolean success=f.delete();
if(success)
System.out.println(fname+" file deleted from local");
}
}
if(file.delete())
System.out.println("Temp folder deleted from local");
}
catch(Exception e){
e.printStackTrace();
}
} // end main
}
You can use Apache FTPClient to do this and all other common commands needed with FTP.
Example to delete a folder:
FTPClient client = new FTPClient();
client.connect(host, port);
client.login(loginname, password);
client.removeDirectory(directoryPathOnServer);
client.disconnect();
Here is a code snippet that deletes all the contents of the directory and the directory itself..
private void deleteDirectory(String path,FTPClient ftpClient) throws Exception{
FTPFile[] files=ftpClient.listFiles(path);
if(files.length>0) {
for (FTPFile ftpFile : files) {
if(ftpFile.isDirectory()){
logger.info("trying to delete directory "+path + "/" + ftpFile.getName());
deleteDirectory(path + "/" + ftpFile.getName(), ftpClient);
}
else {
String deleteFilePath = path + "/" + ftpFile.getName();
logger.info("deleting file {}", deleteFilePath);
ftpClient.deleteFile(deleteFilePath);
}
}
}
logger.info("deleting directory "+path);
ftpClient.removeDirectory(path);
}
If you want to delete an directory in your system
This is an part of example:
File x=new File("C:\Users\satyamahesh\folder");
String[]entries = x.list();
for(String s: entries){
File currentFile = new File(x.getPath(), s);
currentFile.delete();
}
Then your folder is deleted.
If you want test it success or don't success to download a folder
Please test Ad Fundum's answer.
Example to delete a folder: (#SatyaMahesh In this part your code is incorrect and this code used NIO is correct.):
File downloadFile = new File(Local_Dir+ fname.getName());
outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
boolean success = ftp.retrieveFile(FTP_ROOT+fname.getName(), outputStream);
if(success)
count++;
else{
Path path = Paths.get("data/subdir/logging-moved.properties");
try {
Files.delete(path);
} catch (IOException e) {
//deleting file failed
e.printStackTrace();
}
}
Then your folder is deleted.

Error in ZipOutputStream + FTPClient

I've to upload a zip file to ftp server, And here zip file also constructing dynamically.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
public class CommonsNet {
public static void main(String[] args) throws Exception {
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect("127.0.0.1");
client.login("phani", "phani");
String filename = "D://junk.pdf";
fis = new FileInputStream(new File(filename));
byte[] bs = IOUtils.toByteArray(fis);
fis.close();
OutputStream outputStream = client.storeFileStream("remote.zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
zipOutputStream.setLevel(ZipOutputStream.STORED);
addOneFileToZipArchive(zipOutputStream,
"junk.pdf", bs);
zipOutputStream.close();
outputStream.close();
client.logout();
System.out.println("Transfer done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void addOneFileToZipArchive(ZipOutputStream zipStream,
String fileName, byte[] content) throws Exception {
ZipArchiveEntry zipEntry = new ZipArchiveEntry(fileName);
zipStream.putNextEntry(zipEntry);
zipStream.write(content);
zipStream.flush();
zipStream.closeEntry();
}
}
After executing this code the file is successfully created but i am unable to open a file inside archive.
like :
! D:\phani\remote.zip: The archive is corrupt
! D:\phani\remote.zip: Checksum error in C:\Users\BHAVIR~1.KUM\AppData\Local\Temp\Rar$DIa0.489\MCReport.pdf. The file is corrupt
Try adding client.setFileType(FTP.BINARY_FILE_TYPE); just after you have logged in.
I remember that default transfer mode is ASCII, so non-ascii files may result corrupted.

Create intermediate folders if one doesn't exist

I am trying to create a folder for each username a user logs in as. Currently I have
private String destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads
File theFile = new File(destination + username); // will create a sub folder for each user
but the File theFile bit does not create a new folder for the username. How would I do this ?
I have tried
private String destination;
public void File()
{
destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads
File theFile = new File(destination + username); // will create a sub folder for each user (currently does not work, below hopefully is a solution)
theFile.mkdirs();
}
but I need to use the destination later on in the program, how would I do that?
This is my whole code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package richard.fileupload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import java.io.File;
import org.primefaces.event.FileUploadEvent;
#ViewScoped
#ManagedBean(name = "fileUploadController")
public class FileUploadController {
/*
public void handleFileUpload(FileUploadEvent event) {
System.out.println("called");
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
*/
private String username;
private String destination;
#PostConstruct
public void init() {
System.out.println("called get username");
username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
}
public void File() {
destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads
File theFile = new File(destination + username); // will create a sub folder for each user (currently does not work, below hopefully is a solution)
theFile.mkdirs();
}
public File getDirectory(String destination, String username) {
System.out.println("called get directory");
// currently not working, is not calling the username or destination
//set the user directory from the destinarion and the logged user name
File directory = new File(destination, username);
//check if the location exists
if (!directory.exists()) {
//let's try to create it
try {
directory.mkdir();
} catch (SecurityException secEx) {
//handle the exception
secEx.printStackTrace(System.out);
directory = null;
}
}
return directory;
}
public void handleFileUpload(FileUploadEvent event) {
System.out.println("called handle file");
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); //Displays to user on the webpage
FacesContext.getCurrentInstance().addMessage(null, msg);
try {
copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
} catch (IOException e) {
//handle the exception
e.printStackTrace();
}
}
public void copyFile(String fileName, InputStream in) {
try {
// write the inputStream to a FileOutputStream
OutputStream out = new FileOutputStream(new File(destination + fileName)); // cannot find path when adding username atm
System.out.println("Called CopyFile"); //testing
System.out.println(destination + fileName);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
in.close();
out.flush();
out.close();
//make sure new file is created, (displays in glassfish server console not to end user)
System.out.println("New file created!");//testing
} catch (IOException e) {
e.printStackTrace();
FacesMessage error = new FacesMessage("The files were not uploaded!");
FacesContext.getCurrentInstance().addMessage(null, error);
}
}
}
FINAL EDIT (Hopefully)
public void copyFile(String fileName, InputStream in) {
try {
destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads
File theFile = new File(destination + "/" + username);
theFile.mkdirs();// will create a sub folder for each user (currently does not work, below hopefully is a solution) (DOES NOW WORK)
System.out.println("Completed File");
// write the inputStream to a FileOutputStream
OutputStream out = new FileOutputStream(new File(destination + fileName)); // cannot find path when adding username atm
System.out.println("Called CopyFile"); //testing
System.out.println(destination + fileName);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
in.close();
out.flush();
out.close();
//make sure new file is created, (displays in glassfish server console not to end user)
System.out.println("New file created!");//testing
} catch (IOException e) {
e.printStackTrace();
FacesMessage error = new FacesMessage("The files were not uploaded!");
FacesContext.getCurrentInstance().addMessage(null, error);
}
}
}
Just how can i print out the new destination and use this later on as currently it creates the new folder but does not select it to use
EDIT SOLVED THIS TOO :
NewDestination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/" + username;
Added the above code and now it all works
You have to actually call some method to create the directories. Just creating a file object will not create the corresponding file or directory on the file system.
You can use File#mkdirs() method to create the directory: -
theFile.mkdirs();
Difference between File#mkdir() and File#mkdirs() is that, the later will create any intermediate directory if it does not exist.
Use this code spinet for create intermediate folders if one doesn't exist while creating/editing file:
File outFile = new File("/dir1/dir2/dir3/test.file");
outFile.getParentFile().mkdirs();
outFile.createNewFile();
A nice Java 7+ answer from Benoit Blanchon can be found here:
With Java 7, you can use Files.createDirectories().
For instance:
Files.createDirectories(Paths.get("/path/to/directory"));
If you have a large hierarchy of stacked, non-existent directories, you must first call Files.createDirectories(..). For example, in Kotlin it may look like this:
fun File.createFileWithParentDirectories() {
if(this.exists())return
val parent = this.parentFile
if(!parent.exists()) Files.createDirectories(parent.toPath())
this.createNewFile()
}

Java, HttpUrlConnection. Problem with getResponseCode()

I try to create the simplest Simplest WebServer and Client using HTTP. (Please, don't tell me to using Apache HTTPClient).
Client: try to PUT some file to Server.
// **PUT**
if(REQUEST.toUpperCase().equals("PUT")) {
File sourceFile = new File(fileName);
if(!sourceFile.canRead()) {
System.out.println("Have not access to this file...");
return;
}
try {
BufferedInputStream is = new BufferedInputStream(new FileInputStream(sourceFile));
URL url = new URL("http://" + HOST+":"+PORT);
System.setProperty("http.keepAlive", "true");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "Application/octet-stream");
connection.setRequestProperty("Content-Length",Long.toString(sourceFile.length()));
connection.addRequestProperty("Content-disposition","attachment; filename="+fileName);
BufferedOutputStream os = new BufferedOutputStream(connection.getOutputStream());
byte[] buf = new byte[sizeArr];
int r = 1;
while((r = is.read(buf)) > 0) {
os.write(buf, 0, r);
}
os.flush();
os.close();
System.out.println("Waiting for the response...");//this is written to console
System.out.println(connection.getResponseCode());//HERE infinite waiting
is.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
On Server: if Request == PUT, then:
// **PUT**
if (header.toUpperCase().equals("PUT")) {
System.setProperty("http.keepAlive", "true");
String fileName = null;
if((fileName = extract(request.toUpperCase(),"FILENAME=","\n")) == null) {
fileName = "UnknownFile.out";
}
try {
File sourceFile = new File(fileName);
BufferedOutputStream osFile = new BufferedOutputStream
(new FileOutputStream(sourceFile));
byte[] locbuf = new byte[sizeArr];
int locr = 1;
while((locr = is.read(locbuf)) > 0) {
System.out.println("locr= "+locr);//this is written to console
osFile.write(locbuf, 0, locr);
}
System.out.println("Ending to record the data to the file.");
// this is NOT written to console
osFile.flush();
osFile.close();
}
catch(IOException ex) {
os.write(CodeRequest("500 Internal Server Error").getBytes());
os.close();
ex.printStackTrace();
return;
}
System.out.println("Trying to send 200 OK");
os.write(CodeRequest("200 OK").getBytes());
os.flush();
os.close(); // where os = clientSocket.getOutputStream();
}
Why doesn't the Client get a Response from the Server? If I interrupted the Client's infinite loop, then WebServer would correctly record data to file. But Client will never know that his file was normally uploaded to the server. If I comment out this statement on Client:
// System.out.println(connection.getResponseCode());
Then Client correctly exit from loop and ends. But Server doesn't even write to console this:
while((locr = is.read(locbuf)) > 0) {
System.out.println("locr= "+locr);//this is NOT written to console
osFile.write(locbuf, 0, locr);
}
Server ONLY writes this to console this:
localExcString index out of range: -1
without any Error message.
What's wrong?
Your example code for the server doesn't show the declaration and initialisation of 'is'.
However, my guess is that since the session is keep alive the call to is.read() will block until some data arrives. You have set the content length in the client, so I would be expecting to see the read loop complete when that amount of data has been successfully read.
What string is the CodeRequest method returning in the server code? I think that the problem may be that you are not putting a CRLF at the end of the status line and another one at the end of the response header. For details, read the HTTP 1.1 specification.
You need to call URLConnection#getInputStream() after the write to actually send the request (and thus retrieve the response). Only after this point you can request the response status. This is implemented so because it might take longer to build the request body than to actually sending it and also because there's actually no point of having a request if you're not interested in the response.
Edit sorry, I was wrong. The getResponseCode() already implicitly calls getInputStream().
Anyway, I created a small testcase (see SSCCE for more info) and it just works fine. Try it and see if it works in your case as well.
Client:
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test {
public static void main(String[] args) throws Exception {
String data = "Hello!";
URL url = new URL("http://localhost:8080/playground/test");
OutputStream output = null;
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
output = connection.getOutputStream();
output.write(data.getBytes());
System.out.println("Response code: " + connection.getResponseCode());
} finally {
if (output != null) try { output.close(); } catch (IOException ignore) {}
}
}
}
Server:
package mypackage;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test extends HttpServlet {
protected void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
InputStream input = null;
try {
input = request.getInputStream();
int data;
while ((data = input.read()) > -1) {
System.out.write(data);
}
System.out.println();
} finally {
if (input != null) try { input.close(); } catch (IOException ignore) {}
}
}
}
Result should be a Response code: 200 in the client's stdout and a Hello! in the server's stdout.
Hope this helps in pinpointing the cause of your problem.
On Server:
String strFileLen = extract(request.toUpperCase().trim(),"CONTENT-LENGTH:","\n");
long fileLength = 0;
if(strFileLen != null) {
fileLength = Long.parseLong(strFileLen.trim());
System.out.println("fileLength= "+fileLength);
};
byte[] locbuf = new byte[sizeArr];
int locr = 1;long sumLen = 0;
if(fileLength != 0) {
while((sumLen += (locr=is.read(locbuf))) != fileLength) {
System.out.println("sumLen= "+sumLen);
osFile.write(locbuf, 0, locr);
}
}
===================================================
This works well.

Categories