zoukankan      html  css  js  c++  java
  • RestEasy上传文件的工具类

    前言

    RestEasy是比较老的技术,有挺多坑,非必要不建议使用。
    网络上相关的资料也比较少,只能自己封装一个工具类。

    RestEasy上传文件的资料,可以看一下: https://i.cnblogs.com/posts/edit;postId=14590557

    RestEasyUtil

    
    import org.apache.commons.io.IOUtils;
    import org.jboss.resteasy.plugins.providers.multipart.InputPart;
    
    import javax.ws.rs.core.MediaType;
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;
    import java.util.Map;
    
    public class RestEasyUtil {
    
        private RestEasyUtil(){ }
    
        /**
         * 文件名称
         */
        public static final String FILE_NAME = "fileName";
    
        /**
         * 文件对应的字段名称
         */
        public static final String FILE_DATA_NAME = "fileData";
    
    
    
        /**
         * 从formData中获取字段的值
         *
         * @param uploadForm form表单数据
         * @param key 字段名
         * @return
         * @throws IOException
         */
        public static String getValueFromBody(Map<String, List<InputPart>> uploadForm, String key)  {
            return getValueWithDefault(uploadForm, key, "");
        }
    
    
        /**
         * 从formData中获取字段的值,设置默认值。
         *
         * @param uploadForm
         * @param key
         * @param defaultValue
         * @return
         * @throws IOException
         */
        public static String getValueWithDefault(Map<String, List<InputPart>> uploadForm, String key, String defaultValue)  {
            String value = defaultValue;
            try {
                InputPart inputPart = uploadForm.get(key).get(0);
                inputPart.setMediaType(MediaType.TEXT_PLAIN_TYPE);
                value = inputPart.getBodyAsString();
            } catch (Exception e) {
                log.error("getValueFromBody.lack of key: {}", key);
                log.error("getValueFromBody catch exception.", e );
            }
            return value;
        }
    
    
        /**
         * 获取文件的流
         *
         * @param uploadForm form表单数据
         * @param fileDataName 文件对应的字段名称
         * @return
         * @throws IOException
         */
        public static InputStream  getInputStreamFromBody (Map<String, List<InputPart>> uploadForm, String fileDataName) throws IOException {
            List<InputPart> inputParts = uploadForm.get(fileDataName);
            InputPart inputPart = inputParts.get(0);
            return inputPart.getBody(InputStream.class, null);
        }
    
    
    
        /**
         * 获取完整的数据流
         * 默认的数据流只有1024字节,需要重新生成包含所有字节的 ByteArrayInputStream 数据流。
         *
         * @param uploadForm form表单数据
         * @param fileDataName 文件对应的字段名称
         * @return
         * @throws IOException
         */
        public static InputStream getByteArrayInputStream(Map<String, List<InputPart>> uploadForm, String fileDataName) throws IOException {
            InputStream inputStream = getInputStreamFromBody(uploadForm, fileDataName);
            byte[] bytes = IOUtils.toByteArray(inputStream);
            inputStream = new ByteArrayInputStream(bytes);
            return inputStream;
        }
    
    
    
    }
    
    
    
    
  • 相关阅读:
    字符串匹配算法
    C#中窗体的位置和大小
    关于C#值类型,引用类型,值传递,引用传递(转载)
    ArcMap中设置.mxd相对路径
    统计学上的知识
    .NET 数学实现资料(ZZ)
    牛腩新闻系统学习笔记06讲 编写SQLHelper
    DropDownList 控件不能触发SelectedIndexChanged 事件的另一个原因
    牛腩新闻视频 03讲 数据库设计的心得 如何建立外键sql2008的数据库关系图功能
    使用sql server management studio 2008 连接数据库,无法查看数据库,提示 无法为该请求检索数据 错误916
  • 原文地址:https://www.cnblogs.com/expiator/p/14698649.html
Copyright © 2011-2022 走看看