0% found this document useful (0 votes)
7 views16 pages

Cloud - Computing - Practical 6 - MTOM

The document outlines the steps to develop a Java web application using MTOM techniques for downloading and uploading images/videos to a server. It specifies the required software tools, project architecture, and provides code snippets for the web service and JSP page. Additionally, it includes instructions for setting up the project structure and deploying the application.

Uploaded by

vyosim.ruchita.m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

Cloud - Computing - Practical 6 - MTOM

The document outlines the steps to develop a Java web application using MTOM techniques for downloading and uploading images/videos to a server. It specifies the required software tools, project architecture, and provides code snippets for the web service and JSP page. Additionally, it includes instructions for setting up the project structure and deploying the application.

Uploaded by

vyosim.ruchita.m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Practical 6

Develop application to download image/video from server or upload image/video


to server using MTOM techniques
Software Tools Required
 IDE or Code Editor: Netbeans version 8.0.2
 JDK: java jdk version 8

Downloads Required:
 Netbeans 8.0.2: https://dlc-cdn.sun.com/netbeans/8.0.2/final/bundles/netbeans-8.0.2-
windows.exe
 Java JDK 8: https://www.oracle.com/in/java/technologies/javase/javase8u211-later-
archive-downloads.html#license-lightbox

Project Name: Mtomapplication


Project Architecture:
Select Java Web >Web Application

Click Next
Click Next
Click Finish

Expand Project Architecture

Right Click on Mtomapplication Project New > Web Service

Name: ImageWS and Package: mypkg and Finish


Filename: ImageWS.java
/*
* 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 mypkg;

import java.io.*;
import javax.jws.Oneway;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.xml.ws.soap.MTOM;

@MTOM(enabled = true, threshold = 60000)


@WebService(serviceName = "ImageWS")
public class ImageWS {

@WebMethod(operationName = "upload")
@Oneway
public void upload(@WebParam(name = "Filename") String Filename, @WebParam(name =
"ImageBytes") byte[] ImageBytes) {
String filePath = "C:/Picture/upload/" + Filename;
try {
FileOutputStream fos = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(ImageBytes);
bos.close();
System.out.println("Received file: " + filePath);
} catch (Exception ex) {
ex.printStackTrace();
}
}

@WebMethod(operationName = "download")
public byte[] download(@WebParam(name = "Filename") String Filename) {
String filePath = "C:/Picture/upload/" + Filename;
System.out.println("Sending file: " + filePath);
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] fileBytes = new byte[(int) file.length()];
bis.read(fileBytes);
bis.close();
return fileBytes;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}

Save File.
Run Project
Right Click on Mtomapplication > New > Web Service Client.
If not available click on Others

Select on Browse
Package: pkg
Finish
New > JSP

Name: index and click Finish


Filename: index.jsp
<%--
Document : index
Created on : Feb 3, 2025, 1:25:10 PM
Author : User
--%>
<%@page import="java.io.BufferedOutputStream"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.io.File"%>
<%@page import="javax.xml.ws.soap.MTOMFeature"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%-- start web service invocation --%><hr/>
<%
try {
pkg.ImageWS_Service service = new pkg.ImageWS_Service();
pkg.ImageWS port = service.getImageWSPort(new MTOMFeature(60000));
// TODO initialize WS operation arguments here
String filePath="C:/Picture/abcd2.jpg"; // Assuming this is the correct file path
File file=new File(filePath);
FileInputStream fis=new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(fis);
String filename = file.getName();
byte[]imageBytes=new byte[(int)file.length()];
bis.read(imageBytes);
port.upload(filename, imageBytes);
bis.close();
out.println("File uploaded :" + filePath);
} catch (Exception ex) {
// TODO handle custom exceptions here
ex.printStackTrace();
}
%>
<%-- end web service invocation --%><hr/>
<%-- start web service invocation --%><hr/>
<%
try {
pkg.ImageWS_Service service = new pkg.ImageWS_Service();
pkg.ImageWS port = service.getImageWSPort();
// TODO initialize WS operation arguments here
String filename = "abcd2.jpg"; // Assuming this is the correct file name
// Initialize filePath
String filePath = "C:/Picture/download/" + filename; // Assuming this is the correct download
path
// Invoke the download method and get the file bytes
byte[] fileBytes = port.download(filename);
// Write the downloaded bytes to a file
FileOutputStream fos = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(fileBytes);
bos.close();
out.println("File downloaded: " + filePath);
} catch (Exception ex) {
// TODO handle custom exceptions here
ex.printStackTrace();
}
%>
<%-- end web service invocation --%><hr/>
</body>
</html>
Create a Folder Name: Picture inside
Create 2 Folders name: download and upload and an image with name abcd2 or any name with
proper extension
Here image name and extension: abcd2.jpg

Deploy Project

Then Run Project or F6


------------------------------------------------------

You might also like