November 12, 2004

xiaoxiao2021-03-06  72

Implement file upload using FileUpload

A few days ago, I wrote a personal favorites module. I used the fileupload component to implement the upload operation of the file. I have sorted it today. I have shared with everyone. I used to look at it. Although I can implement multiple files, but numbers It is fixed, unable to achieve dynamic increase, even if the form is increased, but the relevant information, such as manual increasing memories, etc., still cannot be implemented. I have to upload it, but finally, when I show the user, it seems like multiple files, huh, huh. All right. For the words, if the fileupload's JAR package has been downloaded, then we can start to start uploading our files.

The first is to write a good JSP page, you need to pay attention to: There must be a File selection box like this:

Then we write this way in Form:

, UPLOADSERVLET is the servlet written by our own, Upload it is used to achieve files, and it is also the focus of we want to say, can you upload, look at it.

/ **

* File Name: UploadServlet.java;

* Package structure: com.zzxy.control;

*

* Copyright Information:

* /

Package com.zzxy.Control;

Import com.zzxy.common.mis_constant;

Import com.zzxy.common.pdkutils;

Import com.zzxy.model.gr.gr_agent_helper;

Import com.zzxy.model.vo.atom.vo_pt_grscwj;

Import java.util. *;

Import java.io. *;

Import java.util.enumeration;

Import java.util.Regex.matcher;

Import java.util.Regex.pattern;

Import javax.servlet.servletconfig;

Import javax.servlet.servletException;

Import javax.servlet.http.httpservletRequest;

Import javax.servlet.http.httpservletResponse;

Import javax.servlet.http.httpservlet;

Import javax.servlet.http.httpsession;

Import org.apache.commons.fileupload. *;

Import com.zzxy.common.secretcoder;

/ **

* Class Name: FileUploadServlet;

* Class Description: Automatic execution file upload, and will automatically route the CONTROLLER and the corresponding Agent components

*

* editor:

* Writing date: 2004.09.09

* Modify:

* Modify date:

2004/09/11

* Modifying reason: If uploaded is empty and uploaded is not a file, if it is empty, you do not perform write file operations.

* 2. If the uploaded file does not exist at all, the "Upload Failed"

* Version:

* /

Public Class UploadServlet

Extends httpservlet {

/ **

* Name: UploadServlet;

* Features:

* Input parameters:

* Return parameters: no

* /

Public UploadServlet () {

}

Private static final string content_type = "text / html; charset = GBK";

Public Void Init (ServletConfig Config) Throws ServleTexception

{

Super.init (config);

}

/ **

* Name: dopost

* Features:

* Enter parameters: httpservletRequest Request

* Httpservletresponse response

* Return parameters: no

* /

Public void dopost (httpservletRequest Request,

Httpservletresponse response) throws oException {

Response.setContentType (Content_Type);

Try {

PrintWriter out = response.getwriter ();

String strsizationmax = "10485760";

String strsizethreshold = "4096";

// Temporary file path

String struploads = this.getServletContext (). GetRealPath ("/ missoc / mydocs");

// Declare DiskfileUpload object

DiskfileUpload Diskfileupload = new diskfileUpload ();

DiskfileUpload.setsization (new long (strsizemax) .longValue ());

DiskfileUpload.setsizethreshold (new integer (strsizethreshold) ;intValue ());

//diskfileupload.setRepositoryPath (STRTMP);

List fileItems = diskfileupload.parsequest (request);

Out.write ("

Out.print (pdkutils.Approot (request));

Out.write ("/ httpController /" ID = / "dataform /" name = / "dataform /"> / r / n ");

String strsCrid = "";

String strscjid = "";

String strgjz = "";

Strings = "";

String strwjlj = "";

String strrequestid = ""; string strflag = "first";

For (int m = 0; M

FileItem fileItem = (fileItem) fileitems.get (m);

FileItem.isformfield ())

{

// Take a hidden domain

IF (FileItem.GetfieldName (). Equals ("SCRID")) {

STRSCRID = fileItem.getstring ();

}

IF (fileitem.getfieldname (). Equals ("scjid")) {

STRSCJID = fileItem.getstring ();

}

IF (FileItem.GetfieldName (). Equals ("gjz")) {

STRGJZ = fileItem.getstring ();

}

IF (fileItem.getfieldname (). Equals ("ms")) {

Strms = fileitem.getstring ();

}

IF (FileItem.GetfieldName (). Equals ("RequestID")) {

Strrequestid = fileItem.getstring ();

}

IF (fileItem.getfieldname (). Equals ("flag")) {

Strflag = fileItem.getstring ();

}

}

/ / Ignore all form information not not the file domain

IF (! fileItem.isformfield ()) {

Strwjlj = fileitem.getname (). Trim ();

File filename = new file (strwjlj);

String filename = filename.getname ();

String strextend = filename

.substring (filename.lastindexof (".")

Filename.Length ());

Long size = fileItem.getsize ();

IF (Strwjlj == Null || Strwjlj.equals (")) && size == 0)

CONTINUE;

Else

{

/ Add records to the database

GR_AGENT_HELPER GR_AGENT_HELPER = New GR_AGENT_HELPER ();

VO_PT_GRSCWJ VO_PT_GRSCWJ = New VO_PT_GRSCWJ ();

VO_PT_GRSCWJ.SETWJID (GR_AGENT_HELPER.CREATEPK (MIS_CONSTANT.KEY_GRBG_GRSC_SCJID);

VO_PT_GRSCWJ.SETSCJID (long.parselong (strscjid));

Filename = filename.ReplaceAll ("'", "'");

VO_PT_GRSCWJ.SETWJMC (FileName);

IF (STRGJZ.EQUALS ("))

STRGJZ = filename;

String startime = getnowtime ();

VO_PT_GRSCWJ.SETGJZ (STRGJZ);

VO_PT_GRSCWJ.SETMS (STRMS);

VO_PT_GRSCWJ.SETWJDX (SIZE); VO_PT_GRSCWJ.SETSCSJ (Long.Parselong (STRTime));

GR_AGENT_HELPER. ADD (VO_PT_GRSCWJ);

///upload files

Struploads = Struploads "//" strsCrid;

IF (! new file (struploads) .Indirectory ())

New file (STRUPLOADS) .mkdirs ();

String stratmp = struploads "//";

DiskFileUpload.setRepositoryPath (StrtMP);

String filenewname = string.valueof (vo_pt_grscwj.getwjid ()) "_" strtime strextend;

Strtmp = startmp filenewname;

String strwjid = String.Valueof (VO_PT_GRSCWJ.GETWJID ());

/

System.out.println ("Preparing for upload files:" strtmp);

//

FileItem.write (New File (Strtmp));

/ / Deliver information on the upload file upwards

Out.print ("

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

New Post(0)