1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > java muti实现图片上传_MutiFileUpload.java 多文件上传

java muti实现图片上传_MutiFileUpload.java 多文件上传

时间:2024-02-06 16:00:43

相关推荐

java muti实现图片上传_MutiFileUpload.java 多文件上传

package mons.file;

import java.io.UnsupportedEncodingException;

.....

/ ^v^ ** * @version 創建時間:Dec 16, 3:26:41 PM

類說明:多文件上傳

*

*/

public class MutiFileUpload extends HttpServlet {

private static final long serialVersionUID =

670829239023754119L;

private long sizeMax = -1; //

上傳文件大小(單位:字節byte),為-1時,無限制

private String encoding = "utf-8";//

字元編碼,當讀取上傳表單的各部分時會用到該encoding

//緩沖值,

默認是1024*10,即10K

private int sizeThreshold =

DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD;

public Map parameters;// 保存表單參數

public Map files;// 保存上傳的檔

public boolean uploadError = false; //

上傳是否有錯,漠認是沒錯false

/^v^**

* 解析request

*

* @param request

*/

public void parse(HttpServletRequest request)

{

parameters = new

HashMap();

files = new HashMap();

// Create a factory for

disk-based file items

DiskFileItemFactory factory =

new DiskFileItemFactory();

// Set factory

constraints

factory.setSizeThreshold(sizeThreshold);

//

factory.setRepository(repository);

// Create a new file upload

handler

ServletFileUpload upload = new

ServletFileUpload(factory);

// Set overall request size

constraint

upload.setSizeMax(sizeMax);

upload.setHeaderEncoding(encoding);

try {

List items =

upload.parseRequest(request);

Iterator

iterator = items.iterator();

while

(iterator.hasNext()) {

FileItem

item = (FileItem) iterator.next();

if

(item.isFormField()) {

String

fieldName = item.getFieldName();

String

value = item.getString();

parameters.put(fieldName,

value);

}

else {

String

fieldName = item.getFieldName();

files.put(fieldName,

item);

}

}

} catch (FileUploadException e)

{

e.printStackTrace();

this.setUploadError(true);//

設為出錯

}

}

/ ^v^**

* 取得非文件表單的值

* @param key

* @return

*/

public String getParameter(String key) {

return (parameters == null) ?

null : (String) parameters.get(key);

}

public String getParameter(String key, String

encoding) {

if (parameters == null)

return

null;

String value = (String)

parameters.get(key);

try {

if (value !=

null) {

value

= new String(value.getBytes("ISO-8859-1"), encoding);

}

} catch

(UnsupportedEncodingException e) {

e.printStackTrace();

}

return value;

}

/^v^**

* 得到上傳檔的檔案名

*

* @param item

*

* @return

*

*/

public String getFileName(FileItem item) {

String fileName =

item.getName();

fileName =

this.replace(fileName, "\", "/");

fileName =

fileName.substring(fileName.lastIndexOf("/") + 1);

return fileName;

}

/^v^**

* 獲得檔擴展

*

* @param item

* @return

*/

public String getFileExtension(FileItem item)

{

String fileName =

getFileName(item);

fileName =

fileName.substring(fileName.lastIndexOf("."));

return fileName;

}

/^v^**

* 驗證單個的文件是否超過最大值

* @param item

* @param defalutMaxSize

* @return true-正常, false-超過最大值

*/

public boolean checkMaxSize(FileItem item, long

defalutMaxSize){

if(defalutMaxSize == -1){

return

true;

}

return (item.getSize()

<= defalutMaxSize);

}

/^v^**

* 字串替

*

* @param source

*

* @param oldString

*

* @param newString

*

* @return

*

*/

public static String replace(String source,

String oldString, String newString) {

StringBuffer output = new

StringBuffer();

int lengthOfSource =

source.length();

int lengthOfOld =

oldString.length();

int posStart = 0;

int pos;

while ((pos =

source.indexOf(oldString, posStart)) >= 0) {

output.append(source.substring(posStart,

pos));

output.append(newString);

posStart =

pos + lengthOfOld;

}

if (posStart <

lengthOfSource) {

output.append(source.substring(posStart));

}

return output.toString();

}

//getter and setter

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。