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类型的一个对象创建完毕.取得这个对象就可以进行一些文件的操作了,比如保存文件,怎么保存文件这里不说了.
创建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类型的一个对象创建完毕.取得这个对象就可以进行一些文件的操作了,比如保存文件,怎么保存文件这里不说了.