struts1保存上传图片及文件--Java类代码

public ActionForward updateLoad(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {
PrintWriter out = response.getWriter();
String  base64 = request.getParameter("base64");
System.out.println(base64);


String localhost=request.getServerName();//localhost
String CreditPlatform=request.getContextPath();//CreditPlatform
int duank=request.getServerPort();//8080
String dirPath = getServlet().getServletContext().getRealPath("/colorlife/upload"); 
Hashtable files = form.getMultipartRequestHandler().getFileElements(); 
List<String> list = new ArrayList<String>();
for(Enumeration e = files.keys(); e.hasMoreElements();) { 
String key = (String) e.nextElement(); 
FormFile formfile = (FormFile) files.get(key); 
String filename = formfile.getFileName().trim(); 
if (!"".equals(filename)) { 
InputStream ins = formfile.getInputStream();
//获取item中的上传文件的输入流
InputStream in = formfile.getInputStream();
//创建一个文件输出流
dirPath=PathUtil.isExist(dirPath,"cllife","car").replace("/", "\\")+filename;
//返回图片路径
// 
String purl = dirPath.substring(dirPath.lastIndexOf("colorlife")-1);

String url="http://"+localhost+":"+duank+CreditPlatform+purl;
url = url.replace("\\", "/"); 
list.add(url);
FileOutputStream out1 = new FileOutputStream(dirPath);
//创建一个缓冲区
byte buffer[] = new byte[1024];
//判断输入流中的数据是否已经读完的标识
int len = 0;
//循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
while((len=in.read(buffer))>0){
//使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
out1.write(buffer, 0, len);
}
//关闭输入流
in.close();
//关闭输出流
out1.close();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值