处理带说明信息的图片与处理文件上传 四(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()  获取文件大小 相当于in.avilivable();
          Returns the size of the file item

如果你上传是一普通的文本元素,则可以通过以下方式获取元素中的数据

<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(String encoding) 可以指定编码格式
          Returns the contents of the file item as a String, using the specified encoding.

 void

write(File file) 直接将文件保存到另一个文件中去。
          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");//默认以ISO方式读取数据

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

              }else{//说明是一个文件

                  String fileName = file.getName();

                  fileName = fileName.substring(fileName.lastIndexOf("\\")+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("\\")+1);

              InputStream in = item.openStream();

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

           }

       }catch(Exception e){

           e.printStackTrace();

       }

    }

}

 

 

 

 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。