老师,我想问一下,你这里有用jsp上传视频文件到myeclipse中,并且获取视频文件路径的代码?
下面是我写的能上传图片的代码,怎样修改能上传视频文件并获取文件路径呢?
jsp文件:
wj.jsp:
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="gb2312"%>
<%
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 'wj.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">
-->
<link type="text/css" rel="stylesheet" href="css/style2.css">
<link type="text/css" rel="stylesheet" href="css/style4.css">
<style type="text/css">
<!--
.c1 {
margin-top: 100px;
font-size:30px;
}
</style>
</head>
<body>
<form action="wjsz1717/acceptFile1717.jsp" method="post" ENCTYPE="multipart/form-data" id="registerForm">
<INPUT type=FILE name="boy" size="45"/>
<button id="submit" type="submit">提交</button>
<button id="submit" type="reset">重置</button>
</form>
</body>
</html>
accepFile1717.jsp:
<%@ page language="java" contentType="text/html" import="java.util.*,java.io.*" pageEncoding="utf-8"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="upload" %>
<%
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 'acceptFile1717.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">
-->
<style type="text/css">
<!--
.c1 {
margin-top: 50px;
font-size:30px;
}
</style>
</head>
<body>
<upload:UpFile subdir="image1"/>
<div class="c1">
<%=message %><br>
<%=fileName %>
<br><img src="image1/<%=fileName %>" >
</div>
</body>
</html>
tag文件:UpFile.tag:
<%@ tag pageEncoding="gb2312" %>
<%@ tag import="java.io.*" %>
<%@ attribute name="subdir" required="true" %>
<%@ variable name-given="message" scope="AT_END" %>
<%@ variable name-given="fileName" scope="AT_END" %>
<%
jspContext.setAttribute("message","");
String fileName=null;
try{
String tempFileName=(String)session.getId();
File file=new File("D:/myeclipse/Test/WebRoot");
String parentDir=file.getAbsolutePath();
String saveDir=parentDir+"/"+subdir;
File dir=new File(saveDir);
dir.mkdir();
File f1=new File(dir,tempFileName);
FileOutputStream o=new FileOutputStream(f1);
InputStream in=request.getInputStream();
byte b[]=new byte[10000];
int n;
while((n=in.read(b))!=-1){
o.write(b,0,n);
}
o.close();
in.close();
RandomAccessFile randomRead=new RandomAccessFile(f1,"r");
int second=1;
String secondLine=null;
while(second<=2){
secondLine=randomRead.readLine();
second++;
}
System.out.println("-----------1:"+secondLine);
int position=secondLine.lastIndexOf("\\");
fileName=secondLine.substring(position+1,secondLine.length()-1);
System.out.println("-----------2:"+fileName);
randomRead.seek(0);
long forthEndPosition=0;
int forth=1;
while((n=randomRead.readByte())!=-1&&(forth<=4)){
if(n=='\n'){
forthEndPosition=randomRead.getFilePointer();
forth++;
}
}
byte cc[]=fileName.getBytes("ISO-8859-1");
fileName=new String(cc);
File f2=new File(dir,fileName);
RandomAccessFile randomWrite=new RandomAccessFile(f2,"rw");
randomRead.seek(randomRead.length());
long endPosition=randomRead.getFilePointer();
long mark=endPosition;
int j=1;
while((mark>=0)&&(j<=6)){
mark--;
randomRead.seek(mark);
n=randomRead.readByte();
if(n=='\n'){
endPosition=randomRead.getFilePointer();
j++;
}
}
randomRead.seek(forthEndPosition);
long startPoint=randomRead.getFilePointer();
while(startPoint<endPosition-1){
n=randomRead.readByte();
randomWrite.write(n);
startPoint=randomRead.getFilePointer();
}
randomWrite.close();
randomRead.close();
jspContext.setAttribute("message","上传成功");
jspContext.setAttribute("fileName",fileName);
f1.delete();
}
catch(Exception ee){
jspContext.setAttribute("message","没有选择文件或上传文件失败");
}
%>
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星