Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Write file in servlet

843842Feb 5 2009 — edited Feb 5 2009
Hi all,
I am developing an application but there is a error in my code i culdnt find so far. Can anyone help?
I want to write to a .xml file in web server with servlet from remote. My code is
private static String message = "Error during Servlet processing";

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        response.setContentType("text/html;charset=UTF-8");
        try {
            int len = request.getContentLength();
            byte[] input = new byte[len];
            ServletInputStream sin = request.getInputStream();
            int c, count = 0;
            while ((c = sin.read(input, count, input.length - count)) != -1) {
                count += c;
            }
            sin.close();
            String inString = new String(input);
            int index = inString.indexOf("/n");
            if (index == -1) {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.getWriter().print(message);
                response.getWriter().close();
                return;
            }
            String user = inString.substring(0, index);
            String data = inString.substring(index + 2);
            //decode application/x-www-form-urlencoded string
//            String decodedUser = URLDecoder.decode(user, "UTF-8");
//            String decodedData = URLDecoder.decode(data, "UTF-8");
//            int i = decodedData.indexOf("/n");
//            String user = decodedData.substring(0, i);
//            String db = decodedData.substring(i + 2, decodedData.length());
            String result = "no";
//            response.setContentType("text/html");
            String filename = "/WEB-INF/" + user + ".xml";
//            String pathName = getServletContext (  ) .getRealPath ( "/" + filename ) ;
//            String contentType = getServletContext (  ) .getMimeType ( pathName ) ;
//            if  ( contentType != null )  
//                response.setContentType ( contentType ) ; 
//            else 
//                response.setContentType ( "application/octet-stream" ) ;
            
            try {
                OutputStream fcheck = null;
                byte[] buf = data.getBytes();
                fcheck = new FileOutputStream(filename);
                for (int i = 0; i < buf.length; i++) {
                    fcheck.write(buf);
}
result = "yes";
} catch (IOException ex) {
Logger.getLogger(UpdateDB.class.getName()).log(Level.SEVERE, null, ex);
result = "no";
}
// ServletContext context = getServletContext();
// set the response code and write the response data
response.setStatus(HttpServletResponse.SC_OK);
OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
writer.write(result);
writer.flush();
writer.close();
} catch (IOException e) {
try {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getWriter().print(e.getMessage());
response.getWriter().close();
} catch (IOException ioe) {
}
}
}
ý am sending data from remote as a string and want to write it in xml. But it is not working. Where am i wrong?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 5 2009
Added on Feb 5 2009
8 comments
458 views