FormFile,FileItem对象的创建

Class类提供了检索关于某个类的信息的方法,以及创建类实例的方法,在java中,每个对象都是某个类的一个实例,java为每个类都保存了一个固定的Class对象,其中包含了这个类的相关信息,这个Class对象反映了这个类,这个对象的信息有:这个类的名字(字符串形式),该类的超类,它所实现的接口,以及用于加载该类的加载器.
创建FileItem的方法我已经写完:
private FileItem createFileItem(String filePath)
    {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        String textFieldName = "textField";
        int num = filePath.lastIndexOf(".");
        String extFile = filePath.substring(num);
        FileItem item = factory.createItem(textFieldName, "text/plain", true,
            "MyFileName" + extFile);
        File newfile = new File(filePath);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        try
        {
            FileInputStream fis = new FileInputStream(newfile);
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 8192))
                != -1)
            {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        }
        catch (IOException e)
        {
            fail("Unexpected IOException" + e);
        }

        return item;

    }

下面是序列化和串行话的函数,
private Object serializeDeserialize(Object target)
    {

        // Serialize the test object
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try
        {
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(target);
            oos.flush();
            oos.close();
        }
        catch (Exception e)
        {
            fail("Exception during serialization: " + e);
        }

        // Deserialize the test object
        Object result = null;
        try
        {
            ByteArrayInputStream bais = new ByteArrayInputStream(baos
                .toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
            result = ois.readObject();
            bais.close();
        }
        catch (Exception e)
        {
            fail("Exception during deserialization: " + e);
        }
        return result;

    }

通过FileInputStream创建一个文件输入流,然后输入到item里面,返回然后再用下面的方法转换成为FormFile类型:
Class parentClass;
        EmpSessionBean userSessionBean = new EmpSessionBean();
        userSessionBean.setCorpId(corpId);
      
            parentClass = Class
                .forName(
                "org.apache.struts.upload.CommonsMultipartRequestHandler");
            Class childClass = parentClass.getDeclaredClasses()[0];
            Constructor c = childClass.getConstructors()[0];
            c.setAccessible(true);
            FileItem aitem = createFileItem("D://a.jpg");
            FileItem anewItem = (FileItem) serializeDeserialize(aitem);
            FormFile file = (FormFile) c.newInstance(new Object[] {anewItem });
到此formFile类型的一个对象创建完毕.取得这个对象就可以进行一些文件的操作了,比如保存文件,怎么保存文件这里不说了.

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值