如何上传图片到服务器

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

<body>

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

</body>

 

 

//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");
 }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值