日期:2014-05-17 浏览次数:20918 次
new File(FileManager.T_MAIL_ATTACHMENT_PATH + folder).mkdirs();//创建文件夹
try
{
ServletContext sc = req.getSession().getServletContext();
UploadReceiver ur = new UploadReceiver(sc);
ur.doPost(req, resp);
} catch (Exception e)
{
e.printStackTrace();
}
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.tempus.common.utils.FileManager;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class UploadReceiver extends HttpServlet
{
private static final long serialVersionUID = 1L;
private static File UPLOAD_DIR = new File(FileManager.T_MAIL_ATTACHMENT_PATH);
private static File TEMP_DIR = new File(FileManager.T_MAIL_TEMP_ATTACHMENT_PATH);
private static String CONTENT_TYPE = "text/html;charset=GBK";
private static String CONTENT_LENGTH = "Content-Length";
private static int RESPONSE_CODE = 200;
// private static File UPLOAD_DIR = new File("uploads");
// private static File TEMP_DIR = new File("uploadsTemp");
// private static String CONTENT_TYPE = "text/plain";
// private static String CONTENT_LENGTH = "Content-Length";
// private static int RESPONSE_CODE = 200;
final Logger log = LoggerFactory.getLogger(UploadReceiver.class);
public UploadReceiver(){}
private ServletContext reqs = null;;
public UploadReceiver(ServletContext sc){
this.reqs = sc;
}
@Override
public void init() throws ServletException
{
UPLOAD_DIR.mkdirs();
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
String contentLengthHeader = req.getHeader(CONTENT_LENGTH);
Long expectedFileSize = StringUtils.isBlank(contentLengthHeader) ? null
: Long.parseLong(contentLengthHeader);
RequestParser requestParser;
try
{
resp.setContentType(CONTENT_TYPE);
resp.setStatus(RESPONSE_CODE);
req.setCharacterEncoding("UTF-8");
if (ServletFileUpload.isMultipartContent(req))
{
requestParser = RequestParser.getInstance(req,
new MultipartUploadParser(req, TEMP_DIR,this.reqs));
doWriteTempFileForPostRequest(requestParser);
writeResponse(resp.getWriter(), null);
}&