• HTTP接受文件的java实现


     1 package TOOLS;
     2 
     3 import java.io.BufferedOutputStream;
     4 import java.io.File;
     5 import java.io.FileOutputStream;
     6 import java.io.IOException;
     7 import java.io.InputStream;
     8 import java.io.OutputStream;
     9 import java.util.HashMap;
    10 import java.util.Map;
    11 
    12 import javax.servlet.http.HttpServletRequest;
    13 import javax.servlet.http.HttpServletResponse;
    14 import org.apache.commons.fileupload.FileItemIterator;
    15 import org.apache.commons.fileupload.FileItemStream;
    16 import org.apache.commons.fileupload.FileUploadException;
    17 import org.apache.commons.fileupload.servlet.ServletFileUpload;
    18 import org.apache.commons.fileupload.util.Streams;
    19 
    20 public  class HTTPPostAcceptPhoto {
    21     
    22     Map<String,String> result = new HashMap<String,String>();
    23     
    24     public Map<String,String> analyze(HttpServletRequest request,
    25             HttpServletResponse response,String imageurl) {
    26         if (ServletFileUpload.isMultipartContent(request)) {// 判断是否含有文件
    27             insertPIC(request,imageurl);
    28         } else {
    29             txthandle(request, response);
    30         }
    31         return result;
    32     }
    33     //写出图片
    34     private void insertPIC(HttpServletRequest request,String url) {
    35 
    36         ServletFileUpload upload = new ServletFileUpload();
    37         FileItemIterator iter;
    38         try {
    39             iter = upload.getItemIterator(request);
    40             while (iter.hasNext()) {
    41                 FileItemStream item = iter.next();
    42                 String name = item.getFieldName();// filename
    43                 InputStream stream = item.openStream();
    44                 System.out.println("文本" + item.isFormField());
    45                 if (item.isFormField()) {// 如果是文本
    46                     txt(result,name, Streams.asString(stream,"utf-8"));
    47                 } else {
    48                     String dir = request.getSession().getServletContext()
    49                             .getRealPath(url);
    50                     OutputStream fileos = null;
    51                     byte[] buff = new byte[1];
    52                     File image =new File(dir, item.getName());
    53                     System.out.println(" item.getName():"+ item.getName());
    54                     fileos = new BufferedOutputStream(new FileOutputStream(
    55                             image));
    56 
    57                     while ((stream.read(buff)) != -1) {
    58                         fileos.write(buff);
    59                     }
    60                     fileos.flush();
    61                     fileos.close();
    62                     result.put("imagename", item.getName());//返回图片名字
    63                 }
    64             }
    65         } catch (FileUploadException e) {
    66             // TODO Auto-generated catch block
    67             System.out.println(e);
    68         } catch (IOException e) {
    69             // TODO Auto-generated catch block
    70             System.out.println(e);
    71         }
    72     }
    73     //有文本时的操作
    74     private  void txt(Map<String,String> result,String name, String asString){
    75         result.put(name, asString);
    76     }
    77     //纯文本操作
    78     public  void txthandle(HttpServletRequest request,
    79             HttpServletResponse response){
    80         result = request.getParameterMap();
    81     }
    82     
    83 }
  • 相关阅读:
    antd Upload的使用
    table 的使用方法
    标题前点的制作
    插件multiBtnList的使用
    render的写法
    数据请求
    实体类为什么使用包装类
    el-table中如何遍历数组中对象里的数组?
    关于hash的描述,hashcode etc
    Java集合框架详解
  • 原文地址:https://www.cnblogs.com/afluy/p/3136188.html
Copyright © 2020-2023  润新知