Web services file transfer example


















QueueUserWorkItem to manage the multithreading. Note that any changes to the number of threads will not take effect after you have begun the transfer because the threads have already been queued.

To upload one or more files, simply click the Upload button and you're away. A progress bar and status message will appear for each file transfer, disappearing as soon as each file is transferred successfully.

If there are errors, such as a file hash difference, the file is left on-screen with the error message. The panel for downloading files is similarly easy to use. It has a list box showing all of the files in the Upload folder on the server; there probably won't be any there by default.

You can refresh the files list at any time with the refresh button. To change the save folder, enter a new path in the text box provided. You can also tick the box for "Login Required" if your website is configured with forms authentication. Normally, you can't use a web service if it is protected by forms authentication. This is because forms authentication is performed via a login ASPX page and an authentication cookie is given to the client browser.

These conditions are not web service friendly. There is a work-around to allow you to protect the web service via forms authentication. It sends HttpWebRequest to the login.

Look in the code-behind of login. To use forms authentication, just change the authentication section of web. The client application will auto-detect if forms authentication is required. If it is, it will tick the box for "Login Required" and focus the username field.

The web service has two main methods: AppendChunk is for uploading a file to the server and DownloadChunk is for downloading from the server. The Windows Forms client application can upload a file by sending all of the chunks one after the other using AppendChunk until the file has been completely sent. It does an MD5 hash on the local file and compares it with the hash of the file on the server to ensure that the contents of the files are identical.

The download code is very similar, the main difference being that the client must know from the server how big the file is so that it can know when to stop requesting chunks. A simplified version of the upload code from the Windows Forms client is shown below. Have a look in the code for Form1. Essentially, a file stream is opened on the client for the duration of the transfer.

Then the first chunk is read into the Buffer byte array. The while loop keeps running until the FileStream. Read method returns 0, i. For each iteration, the buffer is sent directly to the web service as a byte[].

The SentBytes variable is used to report progress to the form. In many Windows Forms applications, regular feedback to the user is very important. Having a responsive and visually communicative application is usually worth a small sacrifice in performance. Obviously, the web services aspect to a chunked file transfer is overhead. The client constructs and sends the SOAP message and then the server receives and parses it before sending the response.

If the chunk size is very small, i. It should be clear then that we should aim for the highest possible chunk size that is within our requirements for quick user interface feedback. I have aimed for each chunk to be completed in milliseconds. You can adjust this setting programmatically before the file transfer. See the PreferredTransferDuration variable in the file transfer object.

The client regulates the chunk size automatically, to ensure that each chunk is completed in the desired time. In order to send files across the wire before, you usually had to open the file, read the contents into a buffer, encode the buffer, and send the encoded data down the wire. Utilities functionality, and either set the return value of the function as a String or write the encoded string to the Response buffer. That's a lot of overhead. NET, you use the File class in the System.

IO namespace to open the file. Using the BinaryReader object's ReadBytes method, you can obtain a byte array that you can use as the return value on the Web method.

Here's the code:. Services; using System. Serialization; using System. Open Server. MapPath filename , FileMode. Open, FileAccess.

Read ; binReader. ReadBytes Convert. ToInt32 binReader. Length ; binReader. CreateNew, FileAccess. ReadWrite ; binWriter. Write buffer ; binWriter. The GetFile method accepts a string parameter, filename. The current virtual directory is used to obtain the file using the filename parameter. The file is opened as a BinaryReader type with Read access. The total bytes of the file are stored to a local variable of type byte[] - a byte array.

The file stream is closed, and the Web method returns the byte array. The PutFile method accepts a byte array, buffer , and a string, filename. A file with the name filename is created as a BinaryWriter type with ReadWrite access.

The byte array is written to the file stream and the file stream is closed. WebMethod; import javax. WebService; import com. FileSendService; import com. File; import java. FileOutputStream; import java. IOException; import java. The build should be successful with war and WSDL files generated.

Testing the Application Now run the application by deploying on Tomcat server. You will get the below wsdl file in the browser: Client Application Now we will see how we can consume the service and send file to the above application. Creating Project Create a maven based web project in Eclipse with the following group and artifact id. Group Id: com.

The stub should be generated and build should be successful. Updating Deployment Descriptor Modify the deployment descriptor file — web. ByteArrayOutputStream; import java. InputStream; import java. UnsupportedEncodingException; import javax.

InputStream; import javax. RequestDispatcher; import javax. ServletException; import javax. MultipartConfig; import javax. WebServlet; import javax. HttpServlet; import javax. HttpServletRequest; import javax.



0コメント

  • 1000 / 1000