mberraba Newbie

Joined: 18 Jan 2012 Posts: 1
|
Posted: Wed Jan 18, 2012 4:34 pm Post subject: Java Servlet : upload and converter un ppt to pdf |
|
|
Hellow,
To upload and convert a ppt to pdf i use this servlet
| Code: | package servlet;
import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.scripting.support.RefreshableScriptTargetSource;
import session.BeanSessionFichierRemote;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import entity.Fichier;
import entity.User;
/**
* Servlet implementation class ajout
*/
public class ajout extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ajout() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String nom = null, login = null ,chemin=null;
// upload image
boolean isMultipart = FileUpload.isMultipartContent(request);
if (!isMultipart) {
request.setAttribute("msg", "Request was not multipart!");
request.getRequestDispatcher("msg.jsp").forward(request, response);
return;
}
DiskFileUpload upload = new DiskFileUpload();
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
String fieldName = item.getFieldName();
if (fieldName.equals("nom"))
nom = item.getString();
if (fieldName.equals("login"))
login = item.getString();
}
else {
File fullFile = new File(item.getName());
String ch=System.getenv("user.home"+"/projet_pad/MyProj/Library/");
File savedFile = new File("C:\\Users\\Administrateur\\projet_pad\\webs\\WebContent\\PptFile", fullFile.getName());
chemin = fullFile.getName();
try {
item.write(savedFile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*****************************************************************************************************************/
/*****************************************************************************************************************/
//**********************Ajout dans la base de donnée*************************************************************//
Hashtable<String, String> h = new Hashtable<String, String>();
h.put("java.naming.factory.initial",
"org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
h.put("java.naming.provider.url", "rmi://localhost:1099");
h.put("java.naming.factory.url.pkgs", "org.objectweb.jonas.naming");
Context ctx;
try {
ctx = new InitialContext(h);
String JNDI_NAME = "session.BeanSessionFichier" + "_"
+ BeanSessionFichierRemote.class.getName() + "@Remote";
BeanSessionFichierRemote ref = (BeanSessionFichierRemote) ctx
.lookup(JNDI_NAME);
System.out.println(login+chemin+nom);
User u=new User();
u.setLogin(login);
Fichier f=new Fichier();
f.setChemin(chemin);
f.setNom(nom);
f.setUser(u);
ref.AddFichier(f);
response.sendRedirect("/webs/GetFichierUser?login="+login+"");
} catch (NamingException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
//******************************************************************************************************//
System.out.println("le nom de slide est :" + chemin);
// String newm = nom.substring(nom.lastIndexOf("."));
String newname = chemin.substring(0, chemin.length() - 4);
// String nom = "cours";
System.out.println("le nouveau nom apré l'exctarction est :" + newname);
// partie convetion au pdf
File inputFile = new File("C:\\Users\\Administrateur\\projet_pad\\webs\\WebContent\\PptFile\\"+newname+".ppt");
System.out.println("Jointure de ppt en cours ...");
File outputFile = new File("C:\\Users\\Administrateur\\projet_pad\\webs\\WebContent\\PdfFile\\"+newname+".pdf");
System.out.println("Creation de pdf en cours ...");
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
System.out.println("Connexion avec OOo (localhost:8100) en cours ...");
try {
connection.connect(); // 170
System.out.println("Connexion éffectuée ... ");
} catch (ConnectException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
e.printStackTrace();
}
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
System.out.println("Convertion en cours ...");
// close the connection
connection.disconnect();
System.out.println("Deconnexion en cours ...");
}
} |
i got this errors :
com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException: connection failed: socket,host=localhost,port=8100,tcpNoDelay=1
com.artofsolving.jodconverter.openoffice.connection.AbstractOpenOfficeConnection.connect(AbstractOpenOfficeConnection.java:81)
servlet.ajout.doPost(ajout.java:170)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
i run the OOe on localhost and 8100,
i have add the jobconverter jars to the buikdpath
i dont know what is the matter !!!
Tagged code part - floris v, moderator |
|