Struts实现文件上传与下载

第一步:编写页面文件

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <
%@includefile="/header.jsp"%>
    <title>struts上传文件</title>
  </head>
  <body>
   <h2>使用
Struts上传文件</h2>
     <hr/>
    <form. action="<%=basePath%>strutsfileupload.do?method=upload" method="post"
           enctype="multipart/form-data">
       <table>
         <tr>
           <td>请选择要上传的文件</td>
           <td>
              <input type="file" name="filePath" size="20"/><br/>
           </td>
         </tr>
         <tr>
            <td>
              <input type="submit" value="上传"/>
            </td>
            <td>
              <a href="<%=basePath%>strutsfileupload.do?method=download&path=fileload/xxxx.txt">我要下载</a>
            </td>
         </tr>
        </table>
    </form>
  </body>
</html>
第二步:struts-config.xml文件,包含form-bean 和action的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "
http://struts.apache.org/dtds/struts-config_1_2.dtd">
 <data-sources />
 <form-beans >
      <form-bean name="fileUploadForm"
            type="com.xinglongjian.struts.fileupload.FileUploadForm" />
 </form-beans>
 <global-exceptions />
 <global-forwards>
  <forward name="error" path="/error/sysError.jsp"></forward>
 </global-forwards>
 <action-mappings>
  <action path="/strutsfileupload" parameter="method" name="fileUploadForm"
            type="com.xinglongjian.struts.fileupload.FileUploadAction" >
              <forward name="success" path="/struts/success.jsp"></forward>
        </action>
 </action-mappings>
</struts-config>

第三步:FileUploadForm.java

public class FileUploadForm. extends ActionForm
{
 private FormFile filePath; //类型为FormFile,变量filePath与页面的file名称一致。

 public FormFile getFilePath()
 {
  return filePath;
 }

 public void setFilePath(FormFile filePath)
 {
  this.filePath = filePath;
 }
 
}

第四步:FileUploadAction.java

public class FileUploadAction extends DispatchAction
{
 /**
  * 上传文件
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return
  * @throws Exception
  */
 public ActionForward upload(ActionMapping mapping, ActionForm. form, HttpServletRequest request, HttpServletResponse response)throws Exception
 {
  FileUploadForm. fileForm=(FileUploadForm)form;
  FormFile file=fileForm.getFilePath();
  String realPath=this.getServlet().getServletContext().getRealPath("/fileload");
  if(file==null)
   return mapping.findForward("error");
  String filename=file.getFileName();
  int size=file.getFileSize();
  if(size>1024*1024)
   return mapping.findForward("error");
  InputStream is=null;
  BufferedOutputStream bs=null;
  try
  {
   //get a inputstream object form. uploadFile
   is=file.getInputStream();
   bs=new BufferedOutputStream(new FileOutputStream(realPath+"/"+filename));
   byte[] buffer=new byte[20480];
   int count=0;
   while((count=is.read())!=-1)
    bs.write(buffer, 0, count);
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  finally
  {
   bs.flush();
   bs.close();
   is.close();
  }
  
  return mapping.findForward("success");
 }
 /**
  * 下载文件
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return
  * @throws Exception
  */
 public ActionForward download(ActionMapping mapping, ActionForm. form, HttpServletRequest request, HttpServletResponse response)throws Exception
 {
  String path=request.getParameter("path");
  String realPath=this.getServlet().getServletContext().getRealPath("/"+path);
  
  File uploadFile=new File(realPath);
  InputStream is=new FileInputStream(uploadFile);
  OutputStream s=response.getOutputStream();
  
  BufferedInputStream bis=new BufferedInputStream(is);
  BufferedOutputStream bos=new BufferedOutputStream(os);
  //弹出下载对话框的
代码
  response.setHeader("Content-disposition", "attachment;filename="+URLEncoder.encode(path,"utf-8"));
  int bytesRead=0;
  byte[] buffer=new byte[8192];
  while((bytesRead=bis.read(buffer, 0, 8192))!=-1)
   bos.write(buffer, 0, bytesRead);
  bos.close();
     is.close();
     bis.close();
     os.close();
  return null;
 }
}

深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值