//创建文件
File file = new File(identityImgZfile);
//上传到服务器中,并修改图片的名字
identityImgZfile = FileHelp.upload(file, file.getName());
//创建身份证反面的文件
File file1 = new File(identityImgFfile);
//上传到服务器中,修改图片名字
identityImgFfile = FileHelp.upload(file1, file1.getName());
/**
*2011-10-20
*��õ�ǰ��Ŀ·��
*author:������
*/
package com.jbit.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class FileHelp {
//获得存放图片项目路径
public static String getPath(){
HttpServletRequest request = ServletActionContext.getRequest();
String realPath = request.getSession().getServletContext().getRealPath("");
return realPath;
}
/* * 获取一个记录编号 * 格式:12 位时间+业务编号+随即数 *
* 例如:100330101028+04+893 (2010-03-30-10:20:28, 业务号是04, 随机数893 )
* *@param btype 业务类型编号 员工业务01 招聘业务02……
* *@return code 构造的一个记录编号
*/
public static String getNo(){
//返回的code String code;
//系统当前时间 12位
SimpleDateFormat sdf=new SimpleDateFormat("yyMMddhhmmss");
String nowDate=sdf.format(new java.util.Date());
//随机数
String iRandom = Math.round(Math.random()*900 )+100 +"";
//整合一个code
return nowDate + iRandom ;
}
//上传
public static String upload(File file,String name) throws Exception{
// 获得上传图片的后缀名
String aa = name.substring(name.lastIndexOf("."));
// 在给定的路径下存放图片信息
String no = FileHelp.getNo();
File parentDir = new File(FileHelp.getPath()+"\\upload", no + aa);
String pname = "upload\\"+ no + aa;
// 读下给定路径的文件
File src = new File(FileHelp.getPath());
// 判断文件是否存在,不存在进行创建
if (!src.exists()) {
src.mkdirs();
}
// 创建输入流
InputStream in = null;
// 创建输出流
OutputStream out = null;
try {
// 上传文件写入请求流中
in = new FileInputStream(file);
// 读取流中的数据
byte[] b = new byte[in.available()];
in.read(b);
// 把本地读到的文件放到输出流里
out = new FileOutputStream(parentDir);
// 写出输出流
out.write(b);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
// 及时关闭输入和输出流
if (in != null)
in.close();
if (out != null)
out.close();
}
return pname;
}
}
下面那个是我的util类 上面是调用方法时候的代码,求救