A few words of thanks would be greatly appreciated.

Java - File Transfer Client-Server

[Java] File Transfer Client-Server


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.


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 = ();  


Solved by Asefthu


A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
This document, titled « Java - File Transfer Client-Server », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).