Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a complete upload file bean

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to write a complete upload file bean", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to write a complete upload file bean"!

Import java.io.*

Import javax.servlet.ServletInputStream

Import javax.servlet.http.HttpServletRequest

Public class transfer_multi {

Public String [] sourcefile = new String [255]; / / Source file name

Public String objectpath = "CRAV /"; / / destination file directory

Public String [] suffix = new String [255]; / / File suffix

Public String [] objectfilename = new String [255]; / / destination file name

Public ServletInputStream sis = null;// input stream

Public String [] description = new String [255]; / / describe the status

Public long size = 100cm 1024ramp / limit size

Private int count = 0 / number of files transferred

Private byte [] b = new byte [4096]; / / byte stream storage array

Private boolean successful = true

Public void setSourcefile (HttpServletRequest request) throws java.io.IOException {

Sis = request.getInputStream ()

Int a = 0

Int k = 0

String s = ""

While ((a = sis.readLine (b.lt.))! =-1) {

S = new String (bpendium 05a)

If ((k = s.indexOf ("filename="))! =-1) {

S = s.substring (kappa 10)

K = s.indexOf (")

S = s.substring (05k)

Sourcefile [count] = s

K = s.lastIndexOf (.)

Suffix [count] = s.substring (kryp1)

System.out.println (suffix [count])

If (canTransfer (count)) transferfile (count)

}

If (! successful) break

}

}

Public int getCount () {

Return count

}

Public String [] getSourcefile () {

Return sourcefile

}

Public void setObjectpath (String objectpath) {

This.objectpath = objectpath

}

Public String getObjectpath () {

Return objectpath

}

Private boolean canTransfer (int I) {

Suffix [I] = suffix [I] .toLowerCase ()

/ / this is what I use to upload pictures. You can change the suffix or not.

If (sourcefile [I] .equals ("") | (! description [I] .equals ("gif") & &! suffix.equals ("jpg") & &! suffix.equals ("jpeg")) {description [I] = "ERR suffix is wrong"; equals}

Else return true

}

Private void transferfile (int I) {

String x = Long.toString (new java.util.Date () .getTime ())

Try {

Objectfilename [I] = x + "." + suffix [I]

FileOutputStream out = new FileOutputStream (objectpath+ object filename [I])

Int a = 0

Int k = 0

Long hastransfered = 0 position / indicates the number of bytes that have been transferred

String s = ""

While ((a = sis.readLine (b.lt.))! =-1) {

S = new String (bpendium 05a)

If ((k = s.indexOf ("Content-Type:"))! =-1) break

}

Sis.readLine (bpen 0.length)

While ((a = sis.readLine (b.lt.))! =-1) {

S = new String (bpendium 05a)

If ((b [0] = = 45) & & (b [1] = = 45) & & (b [2] = = 45) & & (b [3] = = 45) & (b [4] = = 45)) break

Out.write (bpd0pl a)

Hastransfered+=a

If (hastransfered > = size) {

Description [count] = "ERR The file" + sourcefile [count] + "is too large to transfer. The whole process is interrupted."

Successful = false

Break

}

}

If (successful) description [count] = "Right The file" + sourcefile [count] + "has been transfered successfully."

+ + count

Out.close ()

If (! successful) {

Sis.close ()

File tmp = new File (objectpath+ object file name [count-1])

Tmp.delete ()

}

}

Catch (IOException ioe) {

Description [I] = ioe.toString ()

}

}

Public transfer_multi () {

/ / you can build the server upload directory in the builder, or you can build it yourself when javabean is called

SetObjectpath ("/ home/www/jspvhost4/web/popeyelin/images/")

}

}

At this point, I believe you have a deeper understanding of "how to write a complete uploaded file bean". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report