Rising Sun

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            148 隨筆 :: 0 文章 :: 22 評論 :: 0 Trackbacks
          <2006年7月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(10)

          隨筆分類

          隨筆檔案

          相冊

          java--->ajax

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          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 閱讀(917) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 永嘉县| 英山县| 芮城县| 胶南市| 新乐市| 门头沟区| 九江市| 黑水县| 大关县| 阿尔山市| 江都市| 呼伦贝尔市| 右玉县| 嘉兴市| 昌平区| 拜城县| 万全县| 龙陵县| 施甸县| 株洲县| 上饶市| 军事| 呈贡县| 邵阳市| 温州市| 南江县| 肇源县| 塔城市| 奉新县| 元阳县| 东丰县| 沙湾县| 徐汇区| 会宁县| 正镶白旗| 台中县| 安阳县| 内乡县| 会理县| 平乐县| 靖西县|