1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
@WebServlet("/UploadFile")
@MultipartConfig
public class PluploadServlet extends HttpServlet {
static private final long serialVersionUID = 3447685998419256747L;
static private final String RESP_SUCCESS = "{\"jsonrpc\" : \"2.0\", \"result\" : null, \"id\" : \"id\"}";
static private final String RESP_ERROR = "{\"jsonrpc\" : \"2.0\", \"error\" : {\"code\": 101, \"message\": \"Failed to open input stream.\"}, \"id\" : \"id\"}";
static public final String SEP = System.getProperty("file.separator");
static public final String TMP = System.getProperty("java.io.tmpdir");
static public final String JSON = "application/json";
static public final int BUF_SIZE = 4096;
private static Logger logger = Logger.getLogger(PluploadServlet.class.getSimpleName());
private String getFileName(Part part) {
String partHeader = part.getHeader("content-disposition");
logger.info("Part Header = " + partHeader);
for (String cd : part.getHeader("content-disposition").split(";")) {
if (cd.trim().startsWith("filename")) {
return cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
}
}
return null;
}
/**
* Handles an HTTP POST request from Plupload.
*
* @param req
* The HTTP request
* @param resp
* The HTTP response
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Boolean isAuthenticated = (Boolean) req.getSession(true).getAttribute("isAuthenticated");
if (isAuthenticated != null && isAuthenticated) {
BeanClient beanClient = (BeanClient) req.getSession(true).getAttribute("beanClient");
String responseString = RESP_SUCCESS;
try {
for (Part part : req.getParts()) {
String fileName = getFileName(part);
if (fileName != null) {
logger.info(part.getName());
InputStream is = req.getPart(part.getName()).getInputStream();
logger.info("File name : " + fileName);
saveUploadFile(is, fileName, beanClient);
is.close();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
responseString = RESP_ERROR;
}
resp.setContentType(JSON);
byte[] responseBytes = responseString.getBytes();
resp.setContentLength(responseBytes.length);
ServletOutputStream output = resp.getOutputStream();
output.write(responseBytes);
output.flush();
}
}
/**
* Saves the given file item (using the given input stream) to the web
* server's local temp directory.
*
* @param input
* The input stream to read the file from
* @param item
* The multi-part MIME encoded file
* @param beanClient
*/
private void saveUploadFile(InputStream input, String fileName, BeanClient beanClient) throws IOException {
FacesContext fc = FacesContext.getCurrentInstance();
FileUploadBean bean = null;
File localFile = new File(TMP + SEP + fileName);
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] data = new byte[BUF_SIZE];
int count;
while ((count = input.read(data, 0, BUF_SIZE)) != -1) {
output.write(data, 0, count);
}
input.close();
output.flush();
output.close();
}
} |
Partager