JavaWeb同步学习笔记之九十、JavaWeb_文件上传案例_JS代码

JavaWeb_文件上传案例_JS代码

文件上传案例_JS代码

  • 1.在 upload.jsp 页面上使用 jQuery 实现 “新增一个附件”,“删除附件”。但至少需要保留一个,代码实现:
    upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript"
	src="${pageContext.request.contextPath }/scripts/jquery-1.7.2.js"></script>
<script type="text/javascript">
	$(function() {
		
		var i = 2;
		
		$("#addFile").click(function() {
			$(this).parent().parent().before("<tr class='file'><td>File" 
					+ i + ":</td><td><input type='file' name='file" 
					+ i + "' /></td></tr><tr class='desc'><td>Desc" 
					+ i + ":</td><td><input type='text' name='desc" 
					+ i + "' /><button id='delete" 
					+ i + "'>删除</button></td></tr>");
			i++;
			$("#delete" + (i - 1)).click(function() {
				var $tr = $(this).parent().parent();
				$tr.prev("tr").remove();
				$tr.remove();
				
				$(".file").each(function(index) {
					var n = index + 1;
					$(this).find("td:first").text("File" + n);
					$(this).find("td:last input").attr("name", "file" + n);
				});
				
				$(".desc").each(function(index) {
					var n = index + 1;
					$(this).find("td:first").text("Desc" + n);
					$(this).find("td:last input").attr("name", "desc" + n);
					i = ++n;
				});
				
			});
			return false;
		});
	
	});
</script>
</head>
<body>

	<font color="red">${message }</font>

	<form action="uploadServlet" method="post"
		enctype="multipart/form-data">
		<table>
			<tr class="file">
				<td>File1:</td>
				<td><input type="file" name="file1" /></td>
			</tr>
			<tr class="desc">
				<td>Desc1:</td>
				<td><input type="text" name="desc1" /></td>
			</tr>
			<tr>
				<td><input type="submit" id="submit" value="提交" /></td>
				<td><button id="addFile">新增一个附件</button></td>
			</tr>
		</table>
	</form>

</body>
</html>
简单的利用java与js实现文件上传 package com.fendou.myString; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class FileUpload extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean a=ServletFileUpload.isMultipartContent(request); if(a){ FileItemFactory factory=new DiskFileItemFactory(); ServletFileUpload upload=new ServletFileUpload (factory); Iterator items; try{ items=upload.parseRequest (request).iterator(); while(items.hasNext()){ FileItem item=(FileItem) items.next(); if(!item.isFormField()){ String name=item.getName (); String fileName=name.substring(name.lastIndexOf("\\")+1, name.length()); String path=request.getRealPath("file")+File.pathSeparatorChar+fileName; File uploadFile=new File (path); request.getSession ().setAttribute("file", uploadFile); item.write(uploadFile); response.setContentType ("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); // out.print("<font size='2'> 上传的文件为:"+name+"<br>"); // out.print("保存在服务器上 的地址为:"+path+"</font>"); } } }catch(Exception e){ e.printStackTrace(); } } response.sendRedirect("smartupload.jsp"); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值