谁有JSP无组件上传文件代码给小弟一份!如果好我还追加分数 20

 我来答
百度网友27609ef1b6
2009-08-07 · TA获得超过155个赞
知道答主
回答量:239
采纳率:0%
帮助的人:191万
展开全部
JSP页面部分:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.UpFile" %>
<jsp:useBean id="upFile" class="tom.jiafei.UpFile" scope="session" />
<HTML><BODY> <P>选择要上传的文件:<BR>
<FORM action="" method="post" ENCTYPE="multipart/form-data">
<INPUT type=FILE name="boy" size="45">
<BR> <INPUT type="submit" name ="g" value="提交">
</FORM>
<% upFile.setRequest(request);
upFile.setSession(session);
%>
<jsp:getProperty name="upFile" property="upFileMessage"/>
<P>如果上传的是图像文件,可单击超链接查看图像:
<BR><A href="show.jsp"> 查看图像</A>
</BODY></HTML>
处理类:
package tom.jiafei;
import java.io.*;
import javax.servlet.http.*;
public class UpFile
{ HttpServletRequest request;
HttpSession session;
String upFileMessage="";
public void setRequest(HttpServletRequest request)
{ this.request=request;
}
public void setSession(HttpSession session)
{ this.session=session;
}
public String getUpFileMessage()
{ String fileName=null;
try{ String tempFileName=(String)session.getId();//客户的session的id
File f1=new File("D:/tomcat614/Tomcat 6.0/webapps/file/upfile",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 random=new RandomAccessFile(f1,"r");
int second=1; //读出f1的第2行,析取出上传文件的名字:
String secondLine=null;
while(second<=2)
{ secondLine=random.readLine();
second++;
}

//获取第2行中目录符号'\'最后出现的位置
int position=secondLine.lastIndexOf('\\');

//客户上传的文件的名字是:
fileName=secondLine.substring(position+1,secondLine.length()-1);
byte cc[]=fileName.getBytes("ISO-8859-1");
fileName=new String(cc);

session.setAttribute("Name",fileName);//供show.jsp页面使用
random.seek(0); //再定位到文件f1的开头。
//获取第4行回车符号的位置:
long forthEndPosition=0;
int forth=1;
while((n=random.readByte())!=-1&&(forth<=4))
{ if(n=='\n')
{ forthEndPosition=random.getFilePointer();
forth++;
}
}
//根据客户上传文件的名字,将该文件存入磁盘:
File f2= new File("D:/tomcat614/Tomcat 6.0/webapps/file/upfile",fileName);
//File f2= new File("D:/aaa",fileName);
RandomAccessFile random2=new RandomAccessFile(f2,"rw");
//确定出文件f1中包含客户上传的文件的内容的最后位置,即倒数第6行。
random.seek(random.length());
long endPosition=random.getFilePointer();
long mark=endPosition;
int j=1;
while((mark>=0)&&(j<=6))
{ mark--;
random.seek(mark);
n=random.readByte();
if(n=='\n')
{ endPosition=random.getFilePointer();
j++;
}
}
//将random流指向文件f1的第4行结束的位置:
random.seek(forthEndPosition);
long startPoint=random.getFilePointer();
//从f1读出客户上传的文件存入f2(读取从第4行结束位置和倒数第6行之间的内容)
while(startPoint<endPosition-1)
{ n=random.readByte();
random2.write(n);
startPoint=random.getFilePointer();
}
random2.close();
random.close();
f1.delete(); //删除临时文件
upFileMessage=fileName+" Successfully UpLoad";
return upFileMessage;
}
catch(Exception exp)
{ if(fileName!=null)
{ upFileMessage=fileName+" Fail to UpLoad";
return upFileMessage;
}
else
{ upFileMessage="";
return upFileMessage;
}
}
}
}
godcomexxx
2009-08-07 · TA获得超过1205个赞
知道小有建树答主
回答量:651
采纳率:0%
帮助的人:578万
展开全部
问下楼主是用的struts框架吗?
如果是的话首先要修改struts.xml文件
我给出的struts.xml配置文件代码,是以前做的上传视频文件的配置文件,你可以根据自己的需要进行修改
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="messages" />
<constant name="struts.multipart.maxSize" value="104857600"/>
<package name="briup" extends="struts-default">

<action name="upload" class="这里是你定义的上传文件的action">
<interceptor-ref name="fileUpload">

<!-- 配置允许上传的文件大小,单位字节 -->
<param name="maximumSize">1024*1024*1024</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="success" type="chain">list</result>
<result name="input">/index.jsp</result>
<result name="failure" type="chain">list</result>
</action>
<action name="list" class="com.briup.action.ListAction">
<result name="success">/test1/list.jsp</result>
</action>

<action name="play" class="com.briup.action.PlayAction">
<result name="success">/test1/player.jsp</result>
</action>
</package>
</struts>

jsp页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<script type="text/javascript">
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="upload.action" method="post" enctype="multipart/form-data" target="LeftdownFrame">
<input type="file" name="myFile" />
<input type="submit" value="上传" >
</form>
</body>
</html>

UploadAction代码

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;

import com.briup.bean.Vidao;
import com.briup.common.HibernateSessionFactory;
import com.briup.common.transaction.HibernateTransaction;
import com.briup.common.transaction.Transaction;
import com.opensymphony.xwork2.ActionSupport;

public class UpLoadAction extends ActionSupport{
private static final long serialVersionUID = 572146812454l;
private static final int BUFFER_SIZE = 16 * 1024;
private File myFile;
@SuppressWarnings("unused")
private String contentType;
/* 上传文件的文件名 ,该文件名不包括文件的路径 */
private String fileName;
private String imageFileName;
/* 对应jsp页面中的<s:textfield/>标志 */
private String caption;

public void setMyFileContentType(String contentType) {
this .contentType = contentType;
}

public void setMyFileFileName(String fileName) {
this .fileName = fileName;
}

public void setMyFile(File myFile) {
this .myFile = myFile;
}

public File getMyFile() {
return myFile;
}

public String getFileName() {
return fileName;
}

public String getImageFileName() {
return imageFileName;
}

public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}

public String getCaption() {
return caption;
}

public void setCaption(String caption) {
this.caption = caption;
}

//把客户端的文件copy到服务器端指定的路径下
private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public String execute() throws Exception {
Vidao v = new Vidao();
HttpServletRequest request = ServletActionContext.getRequest();

if(myFile!=null){
//imageFileName = getExtention(fileName);
imageFileName = fileName;

File imageFile = new File(ServletActionContext.getServletContext().getRealPath("upload")
+ "/" + imageFileName);
// 获得上传后文件的具体路径
String url = ServletActionContext.getServletContext().getRealPath("upload")+"\\"+imageFileName;
copy(myFile,imageFile);
v.setPath(url);
v.setName(imageFileName);
Transaction tran = new HibernateTransaction();
tran.beginTransaction();
Session session = HibernateSessionFactory.getSession();
session.save(v);
tran.commit();
request.setAttribute("message", "上传视频文件成功!");
return "success";
}
return "failure";
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式