<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Rising Sun

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      148 隨筆 :: 0 文章 :: 22 評論 :: 0 Trackbacks
    package MEDET.servlets;
    import java.net.*;
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class SaveFileServlet extends HttpServlet
    {
    ?? FileWriter savefile;
    ?? String filename = null;
    ?? String value = null;
    ?? /**
    ?? * Handles a POST request
    ?? */
    ?? public void doPost(
    ???????? HttpServletRequest request,
    ???????? HttpServletResponse response)
    ????? throws ServletException, IOException
    ?? {
    ????? PrintWriter out = response.getWriter();
    ????? response.setContentType("text/html");
    ????? //FileWriter savefile;
    ????? try {

    ???????? // Verify the content type

    ???????? String ct = request.getContentType();
    ???????? if (!ct.startsWith("multipart/form-data"))
    ??????????? throw new RuntimeException
    ??????????? ("Invalid content type");

    ???????? // Get the boundary string

    ???????? int p = ct.indexOf("boundary=");
    ???????? if (p == -1)
    ??????????? throw new RuntimeException
    ??????????? ("No boundary string found");

    ???????? p += "boundary=".length();
    ???????? String boundary = "--" + ct.substring(p);
    ???????? String finalBoundary = boundary + "--";

    ???????? // We'll parse the multipart/form-data
    ???????? // with a finite state machine

    ???????? // Define names for the parser states

    ???????? final int INIT = 0;
    ???????? final int READING_HEADERS = 1;
    ???????? final int READING_DATA = 2;

    ???????? int state = INIT;

    ???????? // Read and extract the fields

    ???????? BufferedReader in = request.getReader();
    ???????? main: for (;;) {
    ??????????? String line = in.readLine();
    ??????????? if (line == null)
    ?????????????? break;

    ??????????? switch (state) {

    ?????????????? // State 0: Ignoring everything before
    ?????????????? // the first boundary

    ?????????????? case INIT:
    ????????????????? if (line.startsWith(finalBoundary))
    ???????????????????? break main;
    ????????????????? if (line.startsWith(boundary)) {
    ???????????????????? state = READING_HEADERS;
    ???????????????????? filename = "";
    ???????????????????? value = "";
    ????????????????? }
    ????????????????? break;

    ?????????????? // State 1: Parsing the headers

    ?????????????? case READING_HEADERS:
    ????????????????? if (line.length() == 0)
    ???????????????????? state = READING_DATA;
    ????????????????? else {

    ???????????????????? // Get the field name

    ???????????????????? p = line.indexOf("filename=\"");
    ???????????????????? if (p == -1)
    ??????????????????????? break;
    ???????????????????? p += "filename=\"".length();

    ???????????????????? // ... up to the closing quote.

    ???????????????????? int q = line.indexOf("\"", p);
    ???????????????????? if (q == -1)
    ??????????????????????? break;
    ???????????????????? filename = line.substring(p, q);
    ???????????????????? filename="./config/medet/applications/DefaultWebApp/"+filename.substring(filename.lastIndexOf("\\")+1);
    ???????????????????? savefile=new FileWriter(filename);
    ???????????????????? value = "";
    ????????????????? }
    ????????????????? break;

    ?????????????? // State 2: Reading the data

    ?????????????? case READING_DATA:
    ????????????????? if (line.startsWith(finalBoundary)) {
    ???????????????????? savefile.write(value);
    ???????????????????? savefile.close();
    ???????????????????? break main;
    ????????????????? }
    ????????????????? if (line.startsWith(boundary)) {
    ???????????????????? state = READING_HEADERS;
    ????????????????? }
    ????????????????? else {
    ???????????????????? if (value.length() > 0)
    ??????????????????????? value += "\n";
    ???????????????????? value += line;
    ????????????????? }
    ????????????????? break;
    ??????????? }

    ???????? }
    ???????? // Report the incident number back to the client
    ???????? String[] text = {
    ??????????? "<HTML>",
    ??????????? "<HEAD>",
    ??????????? "<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>",
    ??????????? "<TITLE>文件上傳成功</TITLE>",
    ??????????? "</HEAD>",
    ??????????? "<BODY>",
    ??????????? "<CENTER>",
    ??????????? "<H3>文件上傳成功!</H3>",
    ??????????? "</CENTER>"
    ???????? };
    ???????? for (int i = 0; i < text.length; i++)
    ??????????? out.println(text[i]);
    ??????????? out.println(filename);
    ??????????? out.println("</BODY>");
    ??????????? out.println("</HTML>");
    ????? }

    ????? catch (Exception e) {
    ???????? // Write the exception message
    ???????? out.println("<H3>Error:</H3>");
    ???????? out.println("<PRE>");
    ???????? out.println(e.getMessage());
    ???????? out.println("</PRE>");
    ????? }
    ????? finally {
    ???????? out.flush();
    ????? }
    ?? }
    }
    posted on 2006-07-13 15:01 brock 閱讀(914) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    主站蜘蛛池模板: 亚洲AV无码精品无码麻豆| 国产精品另类激情久久久免费| 亚洲日韩激情无码一区| 中文字幕a∨在线乱码免费看| 亚洲日韩国产精品乱| 人人公开免费超级碰碰碰视频 | 免费观看无遮挡www的视频| 亚洲VA成无码人在线观看天堂| 国产精品网站在线观看免费传媒 | 亚洲日韩国产精品乱| 国产日韩精品无码区免费专区国产| 亚洲中文字幕视频国产| 国产无遮挡又黄又爽免费网站| 亚洲av中文无码乱人伦在线咪咕| 免费看成人AA片无码视频吃奶| 久久99亚洲网美利坚合众国| 五月婷婷综合免费| 亚洲综合色一区二区三区| 国产嫩草影院精品免费网址| 国产黄在线观看免费观看不卡| 亚洲AV人无码综合在线观看| 波多野结衣免费在线观看| 亚洲欧美日韩中文高清www777| 亚洲国产精品丝袜在线观看| 久久成人免费大片| 亚洲校园春色另类激情| 免费一区二区视频| 久久免费线看线看| 亚洲伦理中文字幕| 久久久无码精品亚洲日韩软件| 久久永久免费人妻精品下载| 亚洲熟妇自偷自拍另欧美| 亚洲无码精品浪潮| 亚洲视频免费在线播放| 男男黄GAY片免费网站WWW| 亚洲成AV人片在线观看WWW| 成年午夜视频免费观看视频| 三年片免费观看大全国语| 亚洲精品456人成在线| 在线a亚洲v天堂网2019无码| 三年片在线观看免费大全|