小弟最近正在努力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.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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 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();
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
----------------解决方案--------------------------------------------------------
网上转载,希望你用的上.
程序代码:
文件上传类:MoqUploadBean.java
package net.moq.www;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* Title: 文件上传类
* Description: 既能对文件进行上传,又能取得输入框的值,最多可同时上传255个文件
* Copyright: Copyright (c) 2002
* Company: Tekson
* @author 莫琼
* @version 1.0
*/
public class MoqUploadBean {
private String[] sourceFile = new String[255]; //源文件名
private String[] suffix = new String[255]; //文件后缀名
private String canSuffix = ".gif.jpg.jpeg.png"; //可上传的文件后缀名
private String objectPath = "c:/"; //目标文件目录
private String[] objectFileName = new String[255]; //目标文件名
private ServletInputStream sis = null; //输入流
private String[] description = new String[255]; //描述状态
private long size = 100 * 1024; //限制大小
private int count = 0; //已传输文件数目
private byte[] b = new byte[4096]; //字节流存放数组
private boolean successful = true;
private Hashtable fields = new Hashtable();
public MoqUploadBean() {
}
//设置上传文件的后缀名
public void setSuffix(String canSuffix) {
this.canSuffix = canSuffix;
}
//设置文件保存路径
public void setObjectPath(String objectPath) {
this.objectPath = objectPath;
}
//设置文件保存路径
public void setSize(long maxSize) {
this.size = maxSize;
}
//文件上传处理程序
public void setSourceFile(HttpServletRequest request) throws IOException {
sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ( (k = s.indexOf("filename=\"")) != -1) {
// 取得文件数据
s = s.substring(k + 10);
k = s.indexOf("\"");
s = s.substring(0, k);
sourceFile[count] = s;
k = s.lastIndexOf(".");
suffix[count] = s.substring(k + 1);
if (canTransfer(count)) {
transferFile(count);
}
++count;
} else if ( (k = s.indexOf("name=\"")) != -1) {
// 普通表单输入元素,获取输入元素名字
String fieldName = s.substring(k+6, s.length()-3);
sis.readLine(b, 0, b.length);
StringBuffer fieldValue = new StringBuffer(b.length);
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a-2);
if ( (b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {
break;
} else {
fieldValue.append(s);
}
}
fields.put(fieldName, fieldValue.toString());
}
if (!successful)
break;
}
}
//取得表单元素值
public String getFieldValue(String fieldName) {
if (fields == null || fieldName == null) {
return null;
}
return (String) fields.get(fieldName);
}
//取得上传文件数
public int getCount() {
return count;
}
//取得目标路径
public String getObjectPath() {
return objectPath;
}
//取得源文件名
public String[] getSourceFile() {
return sourceFile;
}
//取得目标文件名
public String[] getObjectFileName() {
return objectFileName;
}
//取得上传状态描述
public String[] getDescription() {
return description;
}
//判断上传文件的类型
private boolean canTransfer(int i) {
suffix[i] = suffix[i].toLowerCase();
//这个是用来传图片的,各位可以把后缀名改掉或者不要这个条件
if (sourceFile[i].equals("") || (!(canSuffix.indexOf("."+suffix[i])>=0))) {
description[i] = "ERR: File suffix is wrong.";
return false;
}
else {
return true;
}
}
//上传文件转换
private void transferFile(int i) {
String x = Long.toString(new java.util.Date().getTime());
try {
objectFileName[i] = x + "." + suffix[i];
FileOutputStream out = new FileOutputStream(objectPath + objectFileName[i]);
int a = 0;
int k = 0;
long hastransfered = 0; //标示已经传输的字节数
String s = "";
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ( (k = s.indexOf("Content-Type:")) != -1) {
break;
}
}
sis.readLine(b, 0, b.length);
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ( (b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {
break;
}
out.write(b, 0, a);
hastransfered += a;
if (hastransfered >= size) {
description[count] = "ERR: The file " + sourceFile[count] +
" is too large to transfer. The whole process is interrupted.";
successful = false;
break;
}
}
if (successful) {
description[count] = "Right: The file " + sourceFile[count] +
" has been transfered successfully.";
}
out.close();
if (!successful) {
sis.close();
File tmp = new File(objectPath + objectFileName[count]);
tmp.delete();
}
}
catch (IOException ioe) {
description[i] = ioe.toString();
}
}
public static void main(String[] args) {
System.out.println("Test OK");
}
}
文件上传调用:MoqUpload.jsp
〈%@ page contentType="text/html; charset=GB2312" %>
〈html>
〈head>
〈title>文件上载〈/title>
〈/head>
〈body>
〈form action="MoqUploadSubmit.jsp" enctype="MULTIPART/FORM-DATA" method="post">
作者姓名:〈input type="text" name="Author" />
〈br />
公司名称:〈input type="text" name="Company" />
〈br />
文件描述:〈input type="text" name="Comment" />
〈br />
选择文件1:〈input type="file" name="filename1" />
〈br />
选择文件2:〈input type="file" name="filename2" />
〈br />
选择文件3:〈input type="file" name="filename3" />
〈br />
选择文件4:〈input type="file" name="filename4" />
〈br />
〈input type="submit" value="上载" />
〈/form>
〈/body>
〈/html>
文件上传提交:MoqUploadSubmit.jsp
〈%@ page contentType="text/html;charset=gb2312"%>
〈jsp:useBean id="fileBean" scope="page" class="net.moq.www.MoqUploadBean" />
〈%
fileBean.setObjectPath("D:\\Temp\\");
fileBean.setSize(10000*1024);
fileBean.setSuffix(".gif.jpg.png.jpge.htm");
fileBean.setSourceFile(request);
String [] saSourceFile = fileBean.getSourceFile();
String [] saObjectFile = fileBean.getObjectFileName();
String [] saDescription = fileBean.getDescription();
int iCount = fileBean.getCount();
String sObjectPath = fileBean.getObjectPath();
for(int i=0;i〈iCount;i++) {
out.println("〈br>源始文件:");
out.println(saSourceFile[i]);
out.println("〈br>目标文件:");
out.println(sObjectPath+saObjectFile[i]);
out.println("〈br>上传说明:");
out.println(saDescription[i]);
out.println("〈br>");
}
out.println("〈br>作者:" + fileBean.getFieldValue("Author"));
out.println("〈br>公司:" + fileBean.getFieldValue("Company"));
out.println("〈br>说明:" + fileBean.getFieldValue("Comment"));
%>
package net.moq.www;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* Title: 文件上传类
* Description: 既能对文件进行上传,又能取得输入框的值,最多可同时上传255个文件
* Copyright: Copyright (c) 2002
* Company: Tekson
* @author 莫琼
* @version 1.0
*/
public class MoqUploadBean {
private String[] sourceFile = new String[255]; //源文件名
private String[] suffix = new String[255]; //文件后缀名
private String canSuffix = ".gif.jpg.jpeg.png"; //可上传的文件后缀名
private String objectPath = "c:/"; //目标文件目录
private String[] objectFileName = new String[255]; //目标文件名
private ServletInputStream sis = null; //输入流
private String[] description = new String[255]; //描述状态
private long size = 100 * 1024; //限制大小
private int count = 0; //已传输文件数目
private byte[] b = new byte[4096]; //字节流存放数组
private boolean successful = true;
private Hashtable fields = new Hashtable();
public MoqUploadBean() {
}
//设置上传文件的后缀名
public void setSuffix(String canSuffix) {
this.canSuffix = canSuffix;
}
//设置文件保存路径
public void setObjectPath(String objectPath) {
this.objectPath = objectPath;
}
//设置文件保存路径
public void setSize(long maxSize) {
this.size = maxSize;
}
//文件上传处理程序
public void setSourceFile(HttpServletRequest request) throws IOException {
sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ( (k = s.indexOf("filename=\"")) != -1) {
// 取得文件数据
s = s.substring(k + 10);
k = s.indexOf("\"");
s = s.substring(0, k);
sourceFile[count] = s;
k = s.lastIndexOf(".");
suffix[count] = s.substring(k + 1);
if (canTransfer(count)) {
transferFile(count);
}
++count;
} else if ( (k = s.indexOf("name=\"")) != -1) {
// 普通表单输入元素,获取输入元素名字
String fieldName = s.substring(k+6, s.length()-3);
sis.readLine(b, 0, b.length);
StringBuffer fieldValue = new StringBuffer(b.length);
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a-2);
if ( (b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {
break;
} else {
fieldValue.append(s);
}
}
fields.put(fieldName, fieldValue.toString());
}
if (!successful)
break;
}
}
//取得表单元素值
public String getFieldValue(String fieldName) {
if (fields == null || fieldName == null) {
return null;
}
return (String) fields.get(fieldName);
}
//取得上传文件数
public int getCount() {
return count;
}
//取得目标路径
public String getObjectPath() {
return objectPath;
}
//取得源文件名
public String[] getSourceFile() {
return sourceFile;
}
//取得目标文件名
public String[] getObjectFileName() {
return objectFileName;
}
//取得上传状态描述
public String[] getDescription() {
return description;
}
//判断上传文件的类型
private boolean canTransfer(int i) {
suffix[i] = suffix[i].toLowerCase();
//这个是用来传图片的,各位可以把后缀名改掉或者不要这个条件
if (sourceFile[i].equals("") || (!(canSuffix.indexOf("."+suffix[i])>=0))) {
description[i] = "ERR: File suffix is wrong.";
return false;
}
else {
return true;
}
}
//上传文件转换
private void transferFile(int i) {
String x = Long.toString(new java.util.Date().getTime());
try {
objectFileName[i] = x + "." + suffix[i];
FileOutputStream out = new FileOutputStream(objectPath + objectFileName[i]);
int a = 0;
int k = 0;
long hastransfered = 0; //标示已经传输的字节数
String s = "";
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ( (k = s.indexOf("Content-Type:")) != -1) {
break;
}
}
sis.readLine(b, 0, b.length);
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ( (b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {
break;
}
out.write(b, 0, a);
hastransfered += a;
if (hastransfered >= size) {
description[count] = "ERR: The file " + sourceFile[count] +
" is too large to transfer. The whole process is interrupted.";
successful = false;
break;
}
}
if (successful) {
description[count] = "Right: The file " + sourceFile[count] +
" has been transfered successfully.";
}
out.close();
if (!successful) {
sis.close();
File tmp = new File(objectPath + objectFileName[count]);
tmp.delete();
}
}
catch (IOException ioe) {
description[i] = ioe.toString();
}
}
public static void main(String[] args) {
System.out.println("Test OK");
}
}
文件上传调用:MoqUpload.jsp
〈%@ page contentType="text/html; charset=GB2312" %>
〈html>
〈head>
〈title>文件上载〈/title>
〈/head>
〈body>
〈form action="MoqUploadSubmit.jsp" enctype="MULTIPART/FORM-DATA" method="post">
作者姓名:〈input type="text" name="Author" />
〈br />
公司名称:〈input type="text" name="Company" />
〈br />
文件描述:〈input type="text" name="Comment" />
〈br />
选择文件1:〈input type="file" name="filename1" />
〈br />
选择文件2:〈input type="file" name="filename2" />
〈br />
选择文件3:〈input type="file" name="filename3" />
〈br />
选择文件4:〈input type="file" name="filename4" />
〈br />
〈input type="submit" value="上载" />
〈/form>
〈/body>
〈/html>
文件上传提交:MoqUploadSubmit.jsp
〈%@ page contentType="text/html;charset=gb2312"%>
〈jsp:useBean id="fileBean" scope="page" class="net.moq.www.MoqUploadBean" />
〈%
fileBean.setObjectPath("D:\\Temp\\");
fileBean.setSize(10000*1024);
fileBean.setSuffix(".gif.jpg.png.jpge.htm");
fileBean.setSourceFile(request);
String [] saSourceFile = fileBean.getSourceFile();
String [] saObjectFile = fileBean.getObjectFileName();
String [] saDescription = fileBean.getDescription();
int iCount = fileBean.getCount();
String sObjectPath = fileBean.getObjectPath();
for(int i=0;i〈iCount;i++) {
out.println("〈br>源始文件:");
out.println(saSourceFile[i]);
out.println("〈br>目标文件:");
out.println(sObjectPath+saObjectFile[i]);
out.println("〈br>上传说明:");
out.println(saDescription[i]);
out.println("〈br>");
}
out.println("〈br>作者:" + fileBean.getFieldValue("Author"));
out.println("〈br>公司:" + fileBean.getFieldValue("Company"));
out.println("〈br>说明:" + fileBean.getFieldValue("Comment"));
%>
----------------解决方案--------------------------------------------------------
回复 2楼 gameohyes
谢谢了,正在研究中 ----------------解决方案--------------------------------------------------------
各位大侠?为什么在jsp页面提交给servlet用request.getparamet取不到值那。谢谢!!!!
----------------解决方案--------------------------------------------------------
用getParameter()
----------------解决方案--------------------------------------------------------