国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

處理帶說明信息的圖片與處理文件上傳 四(62)

2019-11-15 00:13:44
字體:
供稿:網(wǎng)友
處理帶說明信息的圖片與處理文件上傳 四(62)

一 、處理帶說明信息的圖片與處理文件上傳

void

delete() 刪除保存在臨時目錄中的文件。

String

getContentType() 獲取文檔的類型 Returns the content type passed by the browser or null if not defined.

String

getFieldName() 獲取字段的名稱,即name=xxxx Returns the name of the field in the multipart form corresponding to this file item.

<input type=”file” name=”img”/>

InputStream

getInputStream() Returns an InputStream that can be used to retrieve the contents of the file.

String

getName() Returns the original filename in the client's filesystem, as PRovided by the browser (or other client software).

獲取文件名稱。

如果是在IE獲取的文件為 c:/aaa/aaa/xxx.jpg –即完整的路徑。

非IE;文件名稱只是 xxx.jpg

long

getSize() 獲取文件大小 相當(dāng)于in.avilivable(); Returns the size of the file item

如果你上傳是一普通的文本元素,則可以通過以下方式獲取元素中的數(shù)據(jù)

<form enctype=”multipart/form-data”>

<input type=”text” name=”name”/>

String

getString() 用于獲取普通的表單域的信息。 Returns the contents of the file item as a String, using the default character encoding.(IOS-8859-1)

String

getString(Stringencoding) 可以指定編碼格式 Returns the contents of the file item as a String, using the specified encoding.

void

write(Filefile) 直接將文件保存到另一個文件中去。 A convenience method to write an uploaded item to disk.

以下文件用判斷一個fileItem是否是file(type=file)對象或是text(type=text|checkbox|radio)對象:

boolean

isFormField() 如果是text|checkbox|radio|select這個值就是true. Determines whether or not a FileItem instance represents a simple form field.

代碼:

public class UpDescServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");//可以獲取中文的文件名

String path = getServletContext().getRealPath("/up");

DiskFileItemFactory disk =

new DiskFileItemFactory();

disk.setRepository(new File("d:/a"));

try{

ServletFileUpload up =

new ServletFileUpload(disk);

List<FileItem> list = up.parseRequest(request);

for(FileItem file:list){

//第一步:判斷是否是普通的表單項

if(file.isFormField()){

String fileName = file.getFieldName();//<input type="text" name="desc">=desc

String value = file.getString("UTF-8");//默認(rèn)以ISO方式讀取數(shù)據(jù)

System.err.println(fileName+"="+value);

}else{//說明是一個文件

String fileName = file.getName();

fileName = fileName.substring(fileName.lastIndexOf("http://")+1);

file.write(new File(path+"/"+fileName));

System.err.println("文件名是:"+fileName);

System.err.println("文件大小是:"+file.getSize());

file.delete();

}

}

}catch(Exception e){

e.printStackTrace();

}

}

}

二處理文件上傳(性能能夠相對之前提升)

核心點用FileItemIterator it= up.getItemIterator(request);處理文件上傳。

package cn.hx.servlet;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintWriter;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.FileItemIterator;

import org.apache.commons.fileupload.FileItemStream;

import org.apache.commons.fileupload.RequestContext;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.apache.commons.fileupload.servlet.ServletRequestContext;

import org.apache.commons.io.FileUtils;

public class FastServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");

String path = getServletContext().getRealPath("/up");

DiskFileItemFactory disk =

new DiskFileItemFactory();

disk.setRepository(new File("d:/a"));

try{

ServletFileUpload up = new ServletFileUpload(disk);

//以下是迭代器模式

FileItemIterator it= up.getItemIterator(request);

while(it.hasNext()){

FileItemStream item = it.next();

String fileName = item.getName();

fileName=fileName.substring(fileName.lastIndexOf("http://")+1);

InputStream in = item.openStream();

FileUtils.copyInputStreamToFile(in,new File(path+"/"+fileName));

}

}catch(Exception e){

e.printStackTrace();

}

}

}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 乡城县| 蒙自县| 博野县| 久治县| 湄潭县| 香格里拉县| 犍为县| 永州市| 陇南市| 宣恩县| 湟源县| 新建县| 察隅县| 平阴县| 沽源县| 外汇| 怀宁县| 桂平市| 阿拉尔市| 芮城县| 江源县| 开鲁县| 陵川县| 尉犁县| 大姚县| 韶关市| 延庆县| 微山县| 福泉市| 浙江省| 信宜市| 白朗县| 庄河市| 玉门市| 长海县| 历史| 梅州市| 西平县| 光山县| 阿巴嘎旗| 翁牛特旗|