JSP Learning Diary (2) Document Upload

xiaoxiao2021-03-06  18

UploadFile.jsp

==============================================

<% @ Page ContentType = "Text / HTML; Charset = UTF-8"%>

Select the file you want to upload:

File:

Title:

UploadFile2.jsp

==============================================

<% @ Page ContentType = "Text / HTML; Charset = UTF-8"%>

<% @ Page Import = "java.io. *"%>

<%

Try {

InputStream in = Request.getinputStream ();

File file = new file ("g: / jspexercise / file", "uploaded.txt");

FileOutputStream O = New FileOutputStream (file);

Byte [] b = new byte [1000];

Int n;

While ((n = in.read (b))! = -1) {

O.Write (B, 0, N);

}

O. close ();

In.Close ();

}

Catch (IOException E) {

E.PrintStackTrace ();

}

Out.println ("file is uploaded.");

%>


Title: <% = Request.getParameter ("Title")%>
Due to the use of multitype / form-data, you cannot get parameters via request.getParameter ()


At this time, various types of parameters are unified in InputStream, so it can only be to obtain parameters by parsing the input stream

UploadFile3.jsp

==============================================

<% @ Page ContentType = "Text / HTML; Charset = UTF-8"%>

<% @ Page Import = "java.io. *"%>

<% - by parsing temporary file UploadFiles to get the real upload content -%>

<%

Try {

InputStream in = Request.getinputStream ();

File Dir = New File ("g: / jspexercise / file");

File F1 = New File (Dir, "UPLoaded.txt");

RandomaccessFile Random1 = New RandomaccessFile (F1, "R");

String formid = random1.readline ();

IF (FormID == Null) {

Return;

}

Formid = formid.substring (Formid.lastIndexof ('-') 1, Formid.Length ());

Random1.seek (0);

String line = random1.readline ();

While (Line! = null && line.indexof (formid)! = -1) {

// Handle a single item

Line = random1.readline ();

IF (line! = null) {

IF (Line.Indexof ("FileName")! = -1) {

// This form is an uploaded file

INT POS = line.lastIndexof ("//");

String filename = line.substring (POS 1, line.Length () - 1);

FileName = New string (filename.getbytes ("ISO-8859-1"), "UTF-8");

FILE F2 = New File (Dir, FileName);

RandomaccessFile Random2 = New RandomaccessFile (F2, "RW");

Random1.readline (); random1.readline (); // file pointer down two lines long startpos = random1.GetFilePointer (); // File content

Long endpos = 0; // The end point of the file content

While (random1.readline (). indexof (formid) == -1) {// until the start of the next table

Endpos = random1.GetFilePointer () - 2; // Does not include the carriage return

}

/ / Re-create files

Random1.Seek (STARTPOS);

Int n;

While (StartPOS

n = random1.readbyte ();

Random2.write (n);

StartPos = random1.GetFilePointer ();

}

Random2.close ();

Out.print ("
Upload file: filename);

Out.print ("( View )");

Random1.seek (endpos 2); // pointing to the starting line of the next form

Line = random1.readline ();

}

Else {

// This form is the input parameter

String paraName = line.substring (line.indexof ("/") 1, line.Length () - 1);

Random1.readline (); // file pointer moves down

StringBuffer buf = new stringbuffer ();

While ((line = random1.readline ()). Indexof (Formid) == -1) {

BUF.Append (line);

}

String Paravalue = New String (BUF) .Getbytes ("ISO-8859-1"), "UTF-8");

Out.print ("
Enter parameters:" paraName "=" ParaValue);

}

}

}

Random1.close ();

}

Catch (IOException E) {

E.PrintStackTrace ();

}

%>

转载请注明原文地址:https://www.9cbs.com/read-41067.html

New Post(0)