| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1112 人关注过本帖
标题:小弟最近正在努力JSP,遇到个问题,希望大虾们帮下忙,小弟不胜感激!!!文 ...
取消只看楼主 加入收藏
kingyue
Rank: 2
等 级:论坛游民
威 望:1
帖 子:28
专家分:21
注 册:2009-6-17
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
小弟最近正在努力JSP,遇到个问题,希望大虾们帮下忙,小弟不胜感激!!!文件上传问题
上传页面:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="user.file.UpFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body bgcolor="cyan">
<jsp:useBean id="upFile" class="user.file.UpFile" scope="session" />
<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>
查看页面:
<%@ page contentType="text/html; Charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<jsp:useBean id="upFile" class="user.file.UpFile" scope="session" />
<%
    String pic=(String)session.getAttribute("Name");
    out.print(pic);
    out.print("<img src="+pic+" width=200 height=200></img>");
%>
</body>
</html>
下面是java bean文件:
package user.file;
import *;
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();
            File f1=new File("E:\\Mysite",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;
            String secondLine=null;
            while(second<=2){
                secondLine=random.readLine();
                second++;
            }
            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);
            random.seek(0);
            long forthEndPosition=0;
            int forth=1;
            while((n=random.readByte())!=-1&&(forth<=4)){
                if(n=='\n'){
                    forthEndPosition=random.getFilePointer();
                    forth++;
                }
            }
            File f2=new File("E:\\Mysite",fileName);
            RandomAccessFile random2=new RandomAccessFile(f2,"rw");
            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.seek(forthEndPosition);
            long startPoint=random.getFilePointer();
            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;
            }
        }
    }
}
小弟正在苦恼中,歇歇各位大虾了!!!
搜索更多相关主题的帖子: 文件 感激 JSP 
2009-11-13 23:42
kingyue
Rank: 2
等 级:论坛游民
威 望:1
帖 子:28
专家分:21
注 册:2009-6-17
收藏
得分:0 
回复 2楼 gameohyes
谢谢了,正在研究中
2009-11-14 12:32
kingyue
Rank: 2
等 级:论坛游民
威 望:1
帖 子:28
专家分:21
注 册:2009-6-17
收藏
得分:0 
用getParameter()
2009-11-22 19:26
快速回复:小弟最近正在努力JSP,遇到个问题,希望大虾们帮下忙,小弟不胜感激! ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013481 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved