用jsp 怎样实现文件上传

用jsp 怎样实现文件上传,第1张

如果你的jsp应用了struts就方便得多了:
<html:form action="/contractdo" method="post"
enctype="multipart/form-data" >
<tr id="date_1">
<th>1:</th>
<td>
<html:file size="40" property="contractFile1" name="contractForm">
</html:file>
<img src="<%= requestgetContextPath() %>/images/add_icongif" style="cursor:pointer;" onClick="addRowLevel()"/>
</td>
</tr>
<tr id="date_2" style="display:none;">
<th><bean:message key="percontractextraprotocal" />2:</th>
<td>
<html:file size="40" property="contractFile2" name="contractForm" ></html:file>
<img src="<%= requestgetContextPath() %>/images/add_icongif" style="cursor:pointer;" onClick="addRowLevel()"/>
<img src="<%= requestgetContextPath() %>/images/sub_icongif" style="cursor:pointer;" onClick="deleteRowLevel(2)"/>
</td>
</tr>
</html:form>
//script
function addRowLevel(){ }
主要是利用FormFile来获取

如果服务器端程序使用的是struts2框架的话,我会,其他的不会。
struts2:
对于上传,jsp页面只需要有个file类型的表单域,如<input type="file" name="xxx" />
struts2的接收请求的action中再写三个属性与这个表单域的名称对应起来,他们是,File类型的xxx,String类型的xxxFileName,String类型的xxxContentType,并其设置相应的set/get方法。则框架负责接收上传文件的字节流,解析文件名,文件类型,直接使用即可。
对于下载,只需要在action的配置文件中设置如下返回值类型和相应参数:
<result type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename=xxx </param> xxx为下载文件的文件名
</result>
且在action总写一个返回值类型为InputStream的getInputStream方法,此方法返回你要下载的文件的流即可。
ps:其中contentDisposition的配置信息中attachment代表点击下载时浏览器先d出个保存位置的提示框,然后再决定是否下载,默认是inline,即直接打开文件。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13377464.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-24
下一篇 2023-07-24

发表评论

登录后才能评论

评论列表(0条)

保存