如何向服务器上传文件(急,在线等)

sddsdd 2004-03-05 08:54:59
急需实现此功能,但手边的几本书上都没有提及。
还望各位大侠不吝赐教!

*^-^*
...全文
123 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhjzh_zjz 2004-04-07
  • 打赏
  • 举报
回复
package zhong;

import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletException;

public class FileUploadBean{
private static String newline = "\r\n"; //设定换行符
private String uploadDirectory = "."; //默认的保存位置
private String ContentType = ""; //文件类型
private String CharacterEncoding = ""; //编码格式

//设定文件要保存在服务器中的位置
public void setUploadDirectory(String s){
uploadDirectory = s;
}
//提取文件名,本方法是把用户上传的文件按照原名保存
private String getFileName(String s){
int i = s.lastIndexOf("\\");
if(i < 0 || i >= s.length() - 1){
i = s.lastIndexOf("/");
if(i < 0 || i >= s.length() - 1)
return s;
}
return s.substring(i + 1);
}

//得到content-type
public void setContentType(String s){
ContentType = s;
int j;
if((j = ContentType.indexOf("boundary=")) != -1){
ContentType = ContentType.substring(j + 9);
ContentType = "--" + ContentType;
}
}
//得到文件编码格式
public void setCharacterEncoding(String s){
CharacterEncoding = s;
}

public void uploadFile( HttpServletRequest req) throws ServletException, IOException{
setCharacterEncoding(req.getCharacterEncoding());
setContentType(req.getContentType());
uploadFile(req.getInputStream());
}

public void uploadFile( ServletInputStream servletinputstream) throws ServletException, IOException{

String s5 = null;
String filename = null;
byte Linebyte[] = new byte[4096];
byte outLinebyte[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1];

String line;
//得到文件名
while((line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null){
int i = line.indexOf("filename=");
if(i >= 0){
line = line.substring(i + 10);
if((i = line.indexOf("\"")) > 0)
line = line.substring(0, i);
break;
}
}

filename = line;

if(filename != null && !filename.equals("\"")){
filename = getFileName(filename);

String sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
if(sContentType.indexOf("Content-Type") >= 0)
readLine(Linebyte, ai, servletinputstream, CharacterEncoding);

//建立新文件的文件句柄
File file = new File(uploadDirectory, filename);

//建立生成新文件的输出流
FileOutputStream fileoutputstream = new FileOutputStream(file);

while((sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null){
if(sContentType.indexOf(ContentType) == 0 && Linebyte[0] == 45)
break;

if(s5 != null){
//写入新文件
fileoutputstream.write(outLinebyte, 0, ai1[0]);
fileoutputstream.flush();
}
s5 = readLine(outLinebyte, ai1, servletinputstream, CharacterEncoding);
if(s5 == null || s5.indexOf(ContentType) == 0 && outLinebyte[0] == 45)
break;
fileoutputstream.write(Linebyte, 0, ai[0]);
fileoutputstream.flush();
}

byte byte0;
if(newline.length() == 1)
byte0 = 2;
else
byte0 = 1;
if(s5 != null && outLinebyte[0] != 45 && ai1[0] > newline.length() * byte0)
fileoutputstream.write(outLinebyte, 0, ai1[0] - newline.length() * byte0);
if(sContentType != null && Linebyte[0] != 45 && ai[0] > newline.length() * byte0)
fileoutputstream.write(Linebyte, 0, ai[0] - newline.length() * byte0);
fileoutputstream.close();
}
}

//readLine函数把表单提交上来的数据按行读取
private String readLine(byte Linebyte[], int ai[],ServletInputStream servletinputstream,String CharacterEncoding){
try{ //读取一行
ai[0] = servletinputstream.readLine(Linebyte, 0, Linebyte.length);
if(ai[0] == -1)
return null;
}catch(IOException ex){
return null;
}
try{
if(CharacterEncoding == null){
//用默认的编码方式把给定的byte数组转换为字符串
return new String(Linebyte, 0, ai[0]);
}else{
//用给定的编码方式把给定的byte数组转换为字符串
return new String(Linebyte, 0, ai[0], CharacterEncoding);
}
}catch(Exception ex){
return null;
}
}
}

zhjzh_zjz 2004-04-07
  • 打赏
  • 举报
回复
uploadFile.htm
<html>
<body>
<center><b>文件上传示例</b></center>
<br>
<table>
<form method="post" enctype="multipart/form-data" action="uploadfile.jsp">
<tr>
<td> <input type=file size=20 name="fname"> </td>
<td> <input type=Submit value="上传"> </td>
</tr>
</form>
</table>
</body>
</html>

uploadfile.jsp

<%@ page language="java"%>
<%@ page contentType="text/HTML;charset=GB2312"%>
<jsp:useBean id="fub" scope="page" class="zhong.FileUploadBean" />

<%
String Dir = "D:\\upload"; //保存文件的路径,请确保目录存在,或改到其他目录
//通过调用JavaBeans的方法完成上传操作
fub.setUploadDirectory(Dir);
fub.uploadFile(request);
out.print("<center><font color=red>成功上载文件至" + Dir + "</font></center>");
%>

zx4517 2004-04-07
  • 打赏
  • 举报
回复
http://www.tongyi.net/article/20031015/200310153755.shtml
zx4517 2004-04-07
  • 打赏
  • 举报
回复
package package;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

public class upload
{

private static String newline = "\n";
private String uploadDirectory;
private String ContentType;
private String CharacterEncoding;

public upload()
{
uploadDirectory = ".";
ContentType = "";
CharacterEncoding = "";
}

private String getFileName(String s)
{
int i = s.lastIndexOf("\\");
if(i < 0 || i >= s.length() - 1)
{
i = s.lastIndexOf("/");
if(i < 0 || i >= s.length() - 1)
return s;
}
return s.substring(i + 1);
}

public void setUploadDirectory(String s)
{
uploadDirectory = s;
}

public void setContentType(String s)
{
ContentType = s;
int i;
if((i = ContentType.indexOf("boundary=")) != -1)
{
ContentType = ContentType.substring(i + 9);
ContentType = "--" + ContentType;
}
}

public void setCharacterEncoding(String s)
{
CharacterEncoding = s;
}

public String uploadFile(HttpServletRequest httpservletrequest)
throws ServletException, IOException
{
String s = null;
setCharacterEncoding(httpservletrequest.getCharacterEncoding());
setContentType(httpservletrequest.getContentType());
s = uploadFile(httpservletrequest.getInputStream());
return s;
}

public String uploadFile(ServletInputStream servletinputstream)
throws ServletException, IOException
{
String s = null;
String s1 = null;
byte abyte0[] = new byte[4096];
byte abyte1[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1];
String s2;
while((s2 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null)
{
int i = s2.indexOf("filename=");
if(i >= 0)
{
s2 = s2.substring(i + 10);
if((i = s2.indexOf("\"")) > 0)
s2 = s2.substring(0, i);
break;
}
}
s1 = s2;
if(s1 != null && !s1.equals("\""))
{
s1 = getFileName(s1);
String s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding);
if(s3.indexOf("Content-Type") >= 0)
readLine(abyte0, ai, servletinputstream, CharacterEncoding);
File file = new File(uploadDirectory, s1);
FileOutputStream fileoutputstream = new FileOutputStream(file);
while((s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null)
{
if(s3.indexOf(ContentType) == 0 && abyte0[0] == 45)
break;
if(s != null)
{
fileoutputstream.write(abyte1, 0, ai1[0]);
fileoutputstream.flush();
}
s = readLine(abyte1, ai1, servletinputstream, CharacterEncoding);
if(s == null || s.indexOf(ContentType) == 0 && abyte1[0] == 45)
break;
fileoutputstream.write(abyte0, 0, ai[0]);
fileoutputstream.flush();
}
byte byte0;
if(newline.length() == 1)
byte0 = 2;
else
byte0 = 1;
if(s != null && abyte1[0] != 45 && ai1[0] > newline.length() * byte0)
fileoutputstream.write(abyte1, 0, ai1[0] - newline.length() * byte0);
if(s3 != null && abyte0[0] != 45 && ai[0] > newline.length() * byte0)
fileoutputstream.write(abyte0, 0, ai[0] - newline.length() * byte0);
fileoutputstream.close();
}
return s1;
}

private String readLine(byte abyte0[], int ai[], ServletInputStream servletinputstream, String s)
{
ai[0] = servletinputstream.readLine(abyte0, 0, abyte0.length);
if(ai[0] == -1)
return null;
break MISSING_BLOCK_LABEL_27;
Object obj;
obj;
return null;
if(s == null)
return new String(abyte0, 0, ai[0]);
return new String(abyte0, 0, ai[0], s);
obj;
return null;
}

}


JSP页:

<%@page contentType="text/html;charset=gb2312" import="package.upload"%>
<%
String Dir = "c:\dir\upload";
String fn="";
upload upload = new upload();
upload.setUploadDirectory(Dir);
fn=upload.uploadFile(request);
%>

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧