---
title: "如何上传图片到服务器"
author: "yang__yuan"
date: "2014-10-29T11:32:27Z"
tags: []
categories: ["开源软件"]
slug: "untitled"
draft: false
lastmod: "2014-10-29T11:32:27Z"
id: 2149181
url: "https://a--bian.iteye.com/blog/2149181"
---

上传图片到服务器，有很多种方式，然而都是以流的方式将图片保存在服务器指定的目录里。

&lt;body&gt;

 &lt;html:form action="/uploadImageAction" enctype="multipart/form-data" method="post"&gt;  
   &lt;table id="editTable" class="table" height="100%"&gt;  
   &lt;tr&gt;  
   &lt;td style="width: 20%;id="chooseImg"&gt;  
                 请选择变压器图片：&lt;hr/&gt;  
      &lt;input type="file" id="fileId"  name="files" /&gt;  
       &lt;hr/&gt;  
     &lt;input id="sub" type="submit" value="保存图片"/&gt;  
   &lt;/td&gt;  
   &lt;/tr&gt;  
   &lt;/table&gt;  
   &lt;/html:form&gt;

&lt;/body&gt;

 

 

//action中的代码

public class UploadImageAction extends BaseAction{  
 private CommonDAO dao= MsgService.dao;  
 /\**  
  * Method execute  
  * @param mapping  
  * @param form  
  * @param request  
  * @param response  
  * @return ActionForward  
  \*/  
 public ActionForward execute(ActionMapping mapping,  
             ActionForm form,  
             HttpServletRequest request,  
             HttpServletResponse response)  
  throws Exception {  
  //得到提交的form表单  
  String terminalno=request.getParameter("terminalno");  
  UploadImageForm theForm = (UploadImageForm ) form;  
  FormFile file = null;//先定义，防止指向为空  
  try{  
         DiskFileUpload fu = new DiskFileUpload();  
             // 设置最大文件尺寸，这里是50MB  
             fu.setSizeMax(50194304);  
             // 设置缓冲区大小，这里是4kb  
             fu.setSizeThreshold(4096);  
         file = theForm.getFiles();// 取得上传的文 件  
         if(!file.getFileName().equals("")){ //判断上传的文件是否为空  
         SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMdd");// 文件重命名   
         String f = file.getFileName().substring(0,file.getFileName().indexOf("."));  
         String namefile = f;    //+ datename + hour + min + second;  
         String filename = null;  
         InputStream stream = file.getInputStream();// 把文件读入  
             filename = file.getFileName();  
             filename = namefile + filename.substring(filename.indexOf("."));// 保存文件路径和文件名  
             String filePath =request.getRealPath("/")+"images\\\\" +filename;   
            File   fi=new File(filePath);  
             if(!fi.exists()){  
              fi.createNewFile();  
             }  
             fi.canWrite();  
             OutputStream bos = new FileOutputStream(filePath);// 建立一个上传文件的路径  
             int bytesRead = 0;  
             byte\[] buf = new byte\[8192];  
             while ((bytesRead = stream.read(buf, 0, 8192)) != -1) {  
                 bos.write(buf, 0, bytesRead);// 将文件写入服务器  
             }  
             bos.close();  
             stream.close();  
             //如果成功，则将文件的路径保存在数据库中，使用的时候通过数据的库的路径来取  
             String imageaddress="..\\\\images\\\\" +filename;  
             String saveUrl="update LcArTerminal set imageaddress= '"+imageaddress+"' where terminalno='"+terminalno+"'";  
             System.out.println(saveUrl);  
        dao.bulkUpdate(saveUrl);  
           }else{  
             return mapping.findForward("failure");  
           }  
         }catch(Exception e){  
          e.printStackTrace();  
          return mapping.findForward("failure");  
         }  
  return  mapping.findForward("success");  
 }  
}