Java - File Transfer Client-Server

December 2016


[Java] File Transfer Client-Server




Issue


As part of a Web application in Java, I need, via a form, users can send files (any type: Excel workbook, image ...) to the server and the latter are therefore recorded in the application directory.

My problem now is that the files are not fully restored (well I think they are, but I have a problem with writing), I explain: when I upload the file "Blabla. txt ", whose content is" Hello, we are Thursday, July 16, 2009 and the sky is sunny. ", then the file saved on the server is:" Blabla.txt "with content:" oju, naked omsl ed tl 6Jilt20 ileteslil. "may be observed by focusing on this nonsense that a letter was written only two . In the case of an image, I cannot do the test because you cannot view it after it has taken this route.

This finding makes me think that this is an encoding problem, but since I know nothing ... I cannot be sure.

In short, here is my code:
boolean isMultipart FileUpload.isMultipartContent = (request);  
DiskFileUpload DiskFileUpload upload = new ();  

<FileItem> List items = null;  
try {  
upload.parseRequest items = (request);  
} Catch (FileUploadException e) {  
}  
String fileName = ((FileItem) items.get (0)). GetName ();  
fileName = nomFichier.substring (nomFichier.lastIndexOf ("\ \") +1);  

Made InputStream = null;  
did = (InputStream) ((FileItem) items.get (0)). getInputStream ();  
FileOutputStream fos = new FileOutputStream (new File ("MyFolder /" + fileName));  
while (fis.read ()! = -1) {  
fos.write (fis.read ());  
}  

fos.close ();  
fis.close ();  

At the JSP, the form encodes multipart:  

<form method="post" name="formfile" action="/adminnat/updateDocumentation" enctype="multipart/form-data">  
<input type="file" name="fichierdoc">  
<input type="submit" value="Valider">  
</ Form>  


So here, after much research to get to this point, this time I'm dead and my research I do not unlock.
If this inspires you, please give me advice and if I'm wrong about the method and there is a lot easier, do not hesitate! Thank you for reading and good day.

Solution


Ok, I realized just after posting this message:

I do a double fis.read (), so normal that I skipped characters ...

I am so ashamed but I leave this message if it can help some. :)

That corrected the song (just the conditional loop):
int fisread fis.read = ();  
while (fisread! = -1) {  
fos.write (fisread);  
fisread fis.read = ();  
}  

Note


Solved by Asefthu

Related :

This document entitled « Java - File Transfer Client-Server » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.