How to create a file in the server?

Hi,
I'm making a webservice that has to generate a XML file, sign it using another webservice and return this XML. The problem is that i don't know how to create the file in the server throught webservice.
I use this code in the webservice to generate the XML and after I return the XML to client in a byte[]
     static void outputDocumentToFile(Document myDocument, String path) {
        //setup this like outputDocument
          try {
               // XMLOutputter outputter = new XMLOutputter("  ", true);
               XMLOutputter outputter = new XMLOutputter();
               //output to a file
               FileWriter writer = new FileWriter(path);
               outputter.output(myDocument, writer);
               writer.close();
          } catch(java.io.IOException e) {
               e.printStackTrace();
    }But the file is generated in the client-side, how can I generate the file in the server-side?
Thanks very much.

Well the File object doesn't create a file it's simply an abstract representation of file and directory pathnames.
You can try something like this:
<%
try
// get the path to the web server root
// I read somewhere not to use 'getRealPath' for some reason which I
// can't seem to remember :S Hopefull someone can enlighten us :)
String path = application.getRealPath("/project/jsp/demo.txt");
File file = new File(path);
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
writer.println("Hello World!");
writer.close();
catch (Exception e)
System.err.println(e.getMessage());
%>
Hope this helps :)

Similar Messages

  • How to Upload a File on the Server

    Hi,
    I want to upload a file on the server from Windows Mobile Application. Can anyone please suggest me a way to do so?
    Thanks in advance, 
    Saheli Sur

    Following links should also help
    http://blog.anthonybaker.me/2013/06/how-to-upload-file-from-windows-phone.html
    http://stackoverflow.com/questions/19954287/how-to-upload-file-to-server-with-http-post-multipart-form-data/20000831#20000831
    Gaurav Khanna | Microsoft .NET MVP | Microsoft Community Contributor

  • How to upload a file to the server using ajax and struts

    With the following code iam able to upload a file ato the server.
    But my problem is It is working fine if iam doing in my system nd when iam trying to
    access theis application from someother system in our office which are connected through lan
    iam getting an error called 500 i,e internal server error.
    Why it is so???????
    Plz help me????????
    It is required in my project.
    I want the code to access from every system.
    My exact requirement is i have to upload a file to the server and retrive its path and show it in the same page from which we
    have uploaded a file.
    Here the file has to be uploaded to the upload folder which is present in the server.Iam using Tomcat server.
    Any help highly appreciated.
    Thanks in Advance
    This is my input jsp
    filename.jsp
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    <script type="text/javascript">
    alertflag = true;
    var xmlHttp;
    function startRequest(file1)
         if(alertflag)
         alert("file1");
         alert(file1);
    xmlHttp=createXmlHttpRequest();
    var video=document.getElementById("filepath").value;
    xmlHttp.open("POST","FilePathAction.do",true);
    xmlHttp.onreadystatechange=handleStateChange;
    xmlHttp.setRequestHeader('Content-Type', application/x-www-form-urlencoded');
    xmlHttp.send("filepath="+file1);
    function createXmlHttpRequest()
         //For IE
    if(window.ActiveXObject)
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         //otherthan IE
    else if(window.XMLHttpRequest)
    xmlHttp=new XMLHttpRequest();
    return xmlHttp;
    //Next is the function that sets up the communication with the server.
    //This function also registers the callback handler, which is handleStateChange. Next is the code for the handler.
    function handleStateChange()
    var message=" ";
    if(xmlHttp.readyState==4)
         if(alertflag)
              alert(xmlHttp.status);
    if(xmlHttp.status==200)
    if(alertflag)
                                       alert("here");
                                       document.getElementById("div1").style.visibility = "visible";     
    var results=xmlHttp.responseText;
              document.getElementById('div1').innerHTML = results;
    else
    alert("Error loading page"+xmlHttp.status+":"+xmlHttp.statusText);
    </script></head><body><form >
    <input type="file" name="filepath" id="filepath" onchange="startRequest(this.value);"/>
    </form>
    <div id="div1" style="visibility:hidden;">
    </div></body></html>
    The corresponding action class is FIlePathAction
    package actions;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.*;
    public class FilePathAction extends Action{
         public ActionForward execute(ActionMapping mapping, ActionForm form,
                   HttpServletRequest request, HttpServletResponse response)
                   throws IOException, ServletException
              String contextPath1 = "";
              String uploadDirName="";
              String filepath="";
                        System.out.println(contextPath1 );
                        String inputfile = request.getParameter("filepath");
                        uploadDirName = getServlet().getServletContext().getRealPath("/upload");
                        File f=new File(inputfile);
    FileInputStream fis=null;
    FileOutputStream fo=null;
    File f1=new File(uploadDirName+"/"+f.getName());
    fis=new FileInputStream(f);
         fo=new FileOutputStream(f1);
                        try
         byte buf[] = new byte[1024*8]; /* declare a 8kB buffer */
         int len = -1;
         while((len = fis.read(buf)) != -1)
         fo.write(buf, 0, len);
                        catch(Exception e)
                                  e.printStackTrace();
                        filepath=f1.getAbsolutePath();
                        request.setAttribute("filepath", filepath);
                        return mapping.findForward("filepath");
    Action-mappings in struts-config.xml
    <action path="/FilePathAction"
                   type="actions.FilePathAction">
                   <forward name="filepath" path="/dummy.jsp"></forward>
              </action>
    and the dummy.jsp code is
    <%=request.getAttribute("filepath")%>

    MESSAGE FROM THE FORUMS ADMINISTRATORS and COMMUNITY
    This thread will be deleted within 24 business hours. You have posted an off-topic question in an area clearly designated for discussions
    about Distributed Real-time Java. Community members looking to help you with your question won't be able to find it in this category.
    Please use the "Search Forums" element on the left panel to locate a forum based on your topic. A more appropriate forum for this post
    could be one of:
    Enterprise Technologies http://forums.sun.com/category.jspa?categoryID=19
    David Holmes

  • How to Generate PDF file on the server

    Hello Everybody !
    I Need to generate a PDF File on the server, and I'm usin the command :
    "OPEN DATASET p_path FOR OUTPUT IN BINARY MODE message l_verro .
           LOOP AT t_lines.
             TRANSFER t_lines-tdline TO p_path.
           ENDLOOP.
           CLOSE DATASET p_path.  "
    The File was generated, but is corrupt . Don't Open the file, It's wrong.
    When I use the function GUI_DOWNLOAD the file is generated  right. But in the server not.
    Can someone help me please ??
    Best Regards,
    Wagner Duarte

    Hi.
    did you check this post?
    Download PDF File to SAP Application Server
    It doest the same as you are asking.
          " Write PDF Binary Data to App Server
        OPEN DATASET lv_filename FOR OUTPUT IN BINARY MODE.
        IF sy-subrc = 0.
          LOOP AT lines INTO ls_data.
            TRANSFER ls_data TO lv_filename.
          ENDLOOP.
        ENDIF.
        CLOSE DATASET lv_filename.
    Regards
    Miguel

  • How to create .exe file of the java program you create?

    This must be the qustions asked several time on the forums.So here it is again.
    This sounds absurd to some developers here cause you'll say just create jar file and distributre to usr but sometimes there is not possiblity of having runtime environment in windows computers.So if we can bundle the enviroment and the setup together then packing into exe and then distribute like most of commercial java application sounds better.But the problem is how to do that?
    Ok creating exe will increase the size but that doesn't matter,if there is any freeware tool to accomplish just that?

    Check this out:
    http://search.java.sun.com/search/java/index.jsp?qp=%2B
    orum%3A31&nh=10&qt=create+exe+file+program&col=javaforu
    s

  • How to create .lsb files in the LabVIEW6?

    my IDE is VC6.0, how can i create a .lsb files?

    lyq;
    lsb files are the one that LabVIEW uses in a CIN. The specific step that creates the lsb file is a custom step which involve calling a program called "lvsbutil.exe", which must be in your cintools directory where LabVIEW is located. You can configure VC++ to do that extra step.
    Check the manual Using External Code in LabVIEW. Chapter 3 explain all the steps to create your own lsb.
    Best regards;
    Enrique
    www.vartortech.com

  • Very Urjent: How to delete temporary files on the server.

    EBS:
    We are facing a big issue with huge volume of temporary directory.We have created invoice program which uses Bursting concept. Every concurrent program is erroring out. How to delete those files automarically.
    Where these temp files are getting stored. How can we delete them once the concurrent request is completed.
    Please help me..its a huge issue. Thanks in advance.

    Sorry Steven,
    I saw your solution. Thanks for that. But I just want to know, do we have any setups in XML Publisher Administrator or bursting control file to delete the files automatically once the concurrent request is completed.
    And one more thing I found is, In the xml administrator, General properties i have specified the temporary directory as '/tmp'. But all the temporary files are loading in '$XDO_TOP/temp' directory. Why is this..

  • How to create command file for the Database Project?

    Hi All,
    We are using Oracle 10G.
    We are using Database Project in Visual Studio.NET to source control the SQL Scripts of SPs and Tables.
    Now, while creating commad file, it asks me select all the files to be included. thats fine. But after creation, if I open and see...It starts like....
    osql...test1.sql.
    osql...test2.sql
    but, osql is commad of SQL Server not of Oracle...if am not wrong..!
    Is there any settings that can help me create command file that works on Oracle?
    Thanks and Regards
    Senthil

    File>Print
    Choose Adobe PDF as your printer
    Go to Setup
    Go to preferences
    Choose the standard
    Make sure the rest of your settings are correct for the page size etc.
    It's a big screenshot so I've linked instead of posting here.
    Sorry had to blur out some PDF settings for privacy reasons
    http://img339.imageshack.us/img339/6103/37345539.png

  • How to include resource files in the server classpath

    Hello,
    I deployed an EAR on weblogic. I have couple of resource files that I need to put in the server classpath. I get the following error when I try to access the web app.
    java.util.MissingResourceException: Can't find bundle for base name HtmlTemplates, locale en_US
    If I copy the resource files in WEB-INF/classes then the error goes away and the application works well. Can someone please tell me a better way to include the resource files in the WebLogic ?
    Thanks,
    Anu

    Hi Anu,
    Putting the resource files in WEB-INF/classes is the correct and good way!
    Simon

  • How to create html files while the request to a jsp file of same

    Hi..
    i need to create html thumbnails during the runtime for a jsp page. so that i can display it in a table (number of pages.). For that, i need to create html pages for each and every request to that jsp file with different data. please help me in this..
    Thanks
    Suresh S

    I believe you will need to more clearly describe what you are trying to do and what the problem is that you are having before you get a reply to your question.

  • How to create a file for the file format PDF/A?

    I want to create a file,which conform to the standard  PDF/A.
    when i use InDesign to create a pdf file and export it,i find the file format only support PDF/X,
    but i can't find any menu for PDF/A.
    who can help me?
    thank you !

    File>Print
    Choose Adobe PDF as your printer
    Go to Setup
    Go to preferences
    Choose the standard
    Make sure the rest of your settings are correct for the page size etc.
    It's a big screenshot so I've linked instead of posting here.
    Sorry had to blur out some PDF settings for privacy reasons
    http://img339.imageshack.us/img339/6103/37345539.png

  • How to create .sites file after the work is done?

    I've lost one or two of my sites regarding to this problem.
    I just know that I need .site files if you need to come back and modify it later.
    Any way to create domain.site files?

    to retieve file content from the databse i have done this.... but its not working . i m using mysql database... i m getting
    1)BLOB cannot be resolved to a type
    2)The method getBLOB(String) is undefined for the type ResultSet
    3)path cannot be resolved
    anyone pls help me...
    my jsp code is...
    <%
    try
    int returnValue = 0;
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    InputStream is = null;
    OutputStream os = null;
    BLOB blob = null;
    String text;
         text=request.getParameter("text");
         Class.forName("com.mysql.jdbc.Driver");
    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sushikha","root","root");
         final String query = "SELECT data,path FROM pro WHERE data LIKE = " + text;
         conn.setAutoCommit(false);
         stmt = conn.createStatement();
         rs = stmt.executeQuery(query);
         if(rs.next())
         blob = rs.getBLOB("data");
              is = blob.getBinaryStream();
              final String fileName = rs.getString("path");
              final Stri0ng filePath = path + fileName;
              os = new FileOutputStream(filePath);
              final int bufferSize = blob.getBufferSize();
              final byte[] buffer = new byte[bufferSize];
              int bytesRead = 0;
              while ((bytesRead = is.read(buffer)) != -1)
              os.write(buffer, 0, bytesRead);
    else
         returnValue = 1;
         catch(Exception e)
         out.println(e);
    %>
    thanx in advance..sush

  • Batch rename files on the server?

    Can someone explain how to batch rename files on the server ?
    I have 1000 rows each containing these fields
    id (unique key)
    productCode
    filename1
    filename2
    For every row I need to rename the following fields:
    Rename [filename1] to [productCode]_[id]_1
    Rename [filename2] to [productCode]_[id]_2
    also I need to rename the actual file on the server using the above scheme
    Can someone show me the way ? I'm a little rusty with ColdFusion

    So far I've got everything working apart from CFFILE which works only if it finds a corresponding jpeg file on the server. In other words it works perfectly until row 20 of the database because the filename contained in the field doesn't have a matching jpeg file on the server, so CFFILE fails like this...
    Attribute validation error for tag CFFILE.
    The value of the attribute source, which is currently C:\images\DSCN1293.JPG, is invalid.
    The error occurred in C:\Inetpub\wwwroot\link\htdocs\old_apps\psp\updater.cfm: line 63
    61 :                    action = "rename"
    62 :                    source = #my_source1#
    63 :                    destination = #my_destination1#>
    is there a way to carry on looping through the remaining rows and ignore the above fail ?

  • How can I save a file in the server?

    Hello,
    I am writing a Java Web Start program that it will generate a html file, how can I save the generated file in the server, fro example, the directory where I stored my JAR files. Because right now, it will create the html file in the local client mahcine instead of the server. Can anyone tell me how can I do this? Thanks you.

    Yeah, thanks but that's not what I was trying to ask.
    I know that you can save and open a document by performing these three steps:
    1. Confirming to save a file to the Downloads folder
    2. Open the download manager by clicking the temporarily green arrow
    3. Clicking on the downloaded file
    What I would like to know is whether or not it is possible to have Firefox open a saved file ''automatically''. At the moment, if I choose to open it automatically, it is only saved to my temp folder.
    It would be great though if it was saved to my Downloads folder ''and'' opened with the respective program on my pc. Is there a way to do this?

  • How and what to configure in the server configuration files of Tomcat Web ?

    Hi,
    I am using TCP/IP socket connection pooling in my project. For pooling I have written code, I want once the tomcat webserver starts there should be at least 10 instances of connections to be created in the pool. Could any one please tell me how do I do this in the server configuration files (server.xml or web.xml).
    Thanks in Adv.
    JitDev2009

    Take note: the mbaworld.com site has its Tomcat setup/environment poorly configured that the documentation which came along with Tomcat setup became indexed by Google. It has further completely nothing to do with Tomcat itself. Please refer Tomcat's own homepage at [http://tomcat.apache.org] for the documentation.

Maybe you are looking for

  • How to sort out different issues on Satellite A505-S6973

    Can anyone tell me, if the warranty period on my laptop has not passed, can i return my laptop to toshiba and have all my issues sorted out. Would there be a cost to do whatever repairs software related needs be done and how i am supposed to go about

  • Multi Page Doc in OLE Container

    I have a multipage Word Document in an OLE container and I want to be able to scroll through the pages without activating the container by pressing buttons for the next, previous, first and last pages along with knowing that I am on say page 5 of 25

  • How do I reverse the migrate from my PC?

    Had I known that this would be such a disaster I would have never had done it. I was told today that I have to reinstall my OS but an Apple doesnt come with a copy of the OS like a simple crappy windows machine.

  • My nokia / ovi / signed email problem and system f...

    first, I've many problem on ovi-service that I try to talk to local nokia care but problems still exist. so, this is my attempt to directly contact nokia staff or nokia-veterian who may can help me out. second, I'm sorry for my poor english. I'm new

  • Fixing paths after drawing with pen tool?

    Afer you draw a shape with the pen tool and you are not happy with the way you drag the curves and drew the path, do you do anything more with the pen tool at that point or do you later fix it with the Direct Selection tool to manipulate the anchor p