jsp新手 想要完成表单上传文字和图片功能。代码基本完成,可是功能总是成功不了。请高手帮忙

\u5982\u4f55\u5b9e\u73b0JSP\u8868\u5355\u63d0\u4ea4\u548cJS\u7684\u529f\u80fd\uff0c\u4e3a\u4ec0\u4e48\u5b9e\u73b0\u4e86JS\u4f46\u662f\u53c8\u5b9e\u73b0\u4e0d\u4e86\u8868\u5355\u7684\u529f\u80fd\uff1f\u539f\u4ee3\u7801\u5982\u4e0b

\u4e0d\u7528ajax\u4e5f\u53ef\u4ee5\u3002\u3002\u3002\u6211\u641e\u4e2aiframe\u3002\u3002\u5728iframe\u91cc\u63d0\u4ea4\u3002\u3002\u63d0\u4ea4\u65f6\u7528js\u5199\u3002\u3002\u3002document.from.submit();\u518dparent.docuemnt.loaction.href="xxx.jsp";\u8fd9\u6837\u5b50\u53ef\u4ee5\u5427\uff1f\u6211\u8ba9\u4e0a\u5c42\u9875\u9762\u8df3\u8f6c\u3002\u3002\u89c6\u89c9\u4e0a\u770b\u4e0d\u51fa\u6765\u7684\u3002

\u56fe\u7247\u7528\u8868\u5355\u8df3\u8f6c\uff0c\u6587\u5b57\u4fe1\u606f\u7528URL\u4f20\u503c\uff0c\u7136\u540e\u518d\u7f16\u7801\u8f6c\u6362\u4e00\u4e0b\u5c31\u53ef\u4ee5\u4e86\uff0c\u6211\u4ee5\u524d\u4e5f\u6709\u8fd9\u6837\u7684\u95ee\u9898\uff0c\u81ea\u5df1\u60f3\u7684\u4e00\u6bb5\u65f6\u95f4\u641e\u5b9a\u4e86

1.引入jspSmartUpload.jar 这个jar包,放到WebRoot/WEB-INF/lib下面
2在WebRoot 新建一个upload文件夹
test5.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>

<body>
<form action="smartupload.jsp" method="post" enctype="multipart/form-data">
文件标题:<input type="text" name="uname"><br>
图片:<input type="file" name="pic"><br>
<input type="submit" value="上传">
</form>
</body>
</html>

smartupload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<jsp:useBean id="smartupload" class="com.jspsmart.upload.SmartUpload"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'smartupload04.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<%
request.setCharacterEncoding("GBK") ;
smartupload.initialize(pageContext) ; // 初始化上传
smartupload.upload() ; // 准备上传
String name = smartupload.getRequest().getParameter("uname") ;
name = name + "." + smartupload.getFiles().getFile(0).getFileExt() ;
String fileName = this.getServletContext().getRealPath("/") + "upload/" + name ;
smartupload.getFiles().getFile(0).saveAs(fileName) ;
%>

<img src="upload/<%=name%>" width="300" height="200"><%=name %>
</body>
</html>

我空间有,用commons-fileupload的。基本就可以了。
注意看链接里面的注意事项。

..........什么叫代码基本完成?图片通常也是通过文字传值,取值的那边通过request.getParameter()来取值,然后如果是图片的话是在另一个文件夹里,根据取到的地址找到要传的图片

当表单中有文件的时候 一般的request就不能使用了

网上有专门的成品类jspupload.jar 里头
有专门用于有文件上传的页面的读取的类 Request..比系统自带的httpservletrequest功能更强大...

那个组件要自己去网上找

commons-fileupload

扩展阅读:jsp制作表单 ... 怎样通过jsp获得表单数据 ... jsp表单提交 ... jsp九大内置对象基本功能 ... jsp怎么引入图片 ... jsp第一行报错 什么原因 ... jsp文件上传 ... 怎么在jsp中添加图片 ... javascript ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网