2 * Copyright 2010 Blue Lotus Software, LLC.
3 * Copyright 2010 John Yeary <jyeary@bluelotussoftware.com>.
4 * Copyright 2010 Allan O'Driscoll
6 * Dual Licensed MIT and GPL v.2
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 * The GNU General Public License (GPL) Version 2, June 1991
30 * This program is free software; you can redistribute it and/or modify it under the terms of the
31 * GNU General Public License as published by the Free Software Foundation; version 2 of the License.
33 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
34 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
35 * See the GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License along with this program;
38 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 package com.bluelotussoftware.apache.commons.fileupload.example;
43 import java.io.FileNotFoundException;
44 import java.io.FileOutputStream;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.PrintWriter;
48 import javax.servlet.ServletConfig;
49 import javax.servlet.ServletException;
50 import javax.servlet.http.HttpServlet;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53 import org.apache.commons.io.IOUtils;
56 * Reads an <code>application/octet-stream</code> and writes it to a file.
57 * @author John Yeary <jyeary@bluelotussoftware.com>
58 * @author Allan O'Driscoll
61 public class OctetStreamReader extends HttpServlet {
63 private static final long serialVersionUID = 6748857432950840322L;
64 private static final String DESTINATION_DIR_PATH = "files";
65 private static String realPath;
70 * @throws ServletException
73 public void init(ServletConfig config) throws ServletException {
75 realPath = getServletContext().getRealPath(DESTINATION_DIR_PATH) + "/";
79 * Handles the HTTP <code>POST</code> method.
80 * @param request servlet request
81 * @param response servlet response
82 * @throws ServletException if a servlet-specific error occurs
83 * @throws IOException if an I/O error occurs
86 protected void doPost(HttpServletRequest request, HttpServletResponse response)
87 throws ServletException {
89 PrintWriter writer = null;
90 InputStream is = null;
91 FileOutputStream fos = null;
94 writer = response.getWriter();
95 } catch (IOException ex) {
96 log(OctetStreamReader.class.getName() + "has thrown an exception: " + ex.getMessage());
99 String filename = request.getHeader("X-File-Name");
101 is = request.getInputStream();
102 fos = new FileOutputStream(new File(realPath + filename));
103 IOUtils.copy(is, fos);
104 response.setStatus(response.SC_OK);
105 writer.print("{success: true}");
106 } catch (FileNotFoundException ex) {
107 response.setStatus(response.SC_INTERNAL_SERVER_ERROR);
108 writer.print("{success: false}");
109 log(OctetStreamReader.class.getName() + "has thrown an exception: " + ex.getMessage());
110 } catch (IOException ex) {
111 response.setStatus(response.SC_INTERNAL_SERVER_ERROR);
112 writer.print("{success: false}");
113 log(OctetStreamReader.class.getName() + "has thrown an exception: " + ex.getMessage());
118 } catch (IOException ignored) {