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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
package tonpackage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.NullArgumentException;
import org.apache.log4j.Logger;
import gwtop.fwk.exception.CommonException;
/**
* The Class FileUploadServlet.
*/
public abstract class FileUploadServlet extends HttpServlet {
/** The Constant LOG. */
private static final Logger LOG = Logger.getLogger(FileUploadServlet.class);
/**
* {@inheritDoc}
*/
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
super.doGet(req, resp);
}
/**
* {@inheritDoc}
*/
@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
// process only multipart requests
if (ServletFileUpload.isMultipartContent(req)) {
this.process(req, resp);
} else {
resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,
"Request contents type is not supported by the servlet.");
}
}
/**
* Treatment.
* @param ips
* the ips
* @param idMigration
* the id migration
* @return the result treatement
* @throws IOException
* Signals that an I/O exception has occurred.
*/
protected abstract ResultTreatement treatment(final InputStream ips, final Long idBean)
throws IOException;
/**
* Process.
* @param req
* the req
* @param resp
* the resp
* @throws IOException
* Signals that an I/O exception has occurred.
*/
private void process(final HttpServletRequest req, final HttpServletResponse resp)
throws IOException {
// Create a factory for disk-based file items
final FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
final ServletFileUpload upload = new ServletFileUpload(factory);
InputStream stream = null;
Long beanId = null;
// Parse the request
try {
final List<FileItem> items = upload.parseRequest(req);
for (final FileItem item : items) {
// process only file upload - discard other form item types
if (item.isFormField()) {
if ("nomDuChamp".equals(item.getFieldName())) {
LOG.debug("valeurDuChamp=" + item.getString());
beanId = Long.parseLong(item.getString());
}
continue;
}
String fileName = item.getName();
// get only the file name not whole path
if (fileName != null) {
fileName = FilenameUtils.getName(fileName);
}
stream = item.getInputStream();
}
if (null == stream) {
throw new NullArgumentException("fichier");
}
final ResultTreatement result = this.treatment(stream, beanId);
resp.setStatus(HttpServletResponse.SC_CREATED);
resp.getWriter().print("Du Texte à afficher\n");
resp.flushBuffer();
} catch (final Exception e) {
if (stream != null) {
stream.close();
}
final CommonException transform = ExceptionHelper.transform(e);
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Une erreure est survenue : " + transform.getMessage());
}
}
} |
Partager