| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 488 人关注过本帖
标题:上传的问题
只看楼主 加入收藏
smoon
Rank: 1
等 级:禁止访问
帖 子:461
专家分:0
注 册:2006-4-29
收藏
 问题点数:0 回复次数:2 
上传的问题

在网上找的一个上传的代码。。
就是长传的文件不是在项目名下 是在tomcat下。。tomcat在c盘。。。项目在d:盘,。。。用的是netbeans
路径写的是。。/upload。。。。大家看看我该怎么改把东西才可以上传到。。项目名下的目录里
这个是代码。。。
=============================================================================
一个文件上传JAVABEAN


package com.upload;

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

public class upload{
private static String newline = "\n";
private String uploadDirectory = ".";
private String ContentType = "";
private String 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 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(String parent, String child)
//Creates a new File instance from a parent pathname string
//and a child pathname string.
File file = new File(uploadDirectory, filename);

//FileOutputStream(File file)
//Creates a file output stream to write to the file represented
//by the specified File object.
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){
//write(byte[] b, int off, int len)
//Writes len bytes from the specified byte array starting
//at offset off to this file output stream.
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();
}
}

private String readLine(byte Linebyte[], int ai[],
ServletInputStream servletinputstream,
String CharacterEncoding){

try{
//readLine(byte[] buffer, int offset, int length)
//Reads a line from the POST data.
ai[0] = servletinputstream.readLine(Linebyte, 0, Linebyte.length);
if(ai[0] == -1)
return null;
}catch(IOException _ex){
return null;
}
try{
if(CharacterEncoding == null){
//用缺省的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes, int offset, int length)
return new String(Linebyte, 0, ai[0]);
}else{
//用给定的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes, int offset, int length, String enc)
return new String(Linebyte, 0, ai[0], CharacterEncoding);
}
}catch(Exception _ex){
return null;
}
}
/*
public int readLine(byte[] buffer,
int offset,
int length) throws java.io.IOException
从POST来的数据中读一行
参数:
buffer - buffer to hold the line data
offset - offset into the buffer to start
length - maximum number of bytes to read.
Returns:
number of bytes read or -1 on the end of line.
*/
}


upload.jsp
<%@page import="com.upload.upload"%>
<%
String Dir = "。。/upload";

upload upload = new upload();
upload.setUploadDirectory(Dir);
upload.uploadFile(request);
out.print("<center><font color=red>成功上载文件至" + Dir + "</font></center>");
%>


upload.html
<center>File upload test</center>
<br>
<table><tr>
<form method="post" enctype="multipart/form-data" action="upload.jsp">
<td> <input type=file size=20 name="fname"> </td>
<td> <input type=Submit value=Upload> </td> </form>
</tr></table>

搜索更多相关主题的帖子: package import 网上 项目 
2007-01-04 11:21
rainic
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2367
专家分:0
注 册:2005-8-9
收藏
得分:0 
呵呵,不太懂,我都是用JavaSmartUpload上传的。。

2007-01-04 11:26
angeloc
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:36
帖 子:1353
专家分:0
注 册:2006-11-21
收藏
得分:0 
以下是引用smoon在2007-1-4 11:21:33的发言:

在网上找的一个上传的代码。。
就是长传的文件不是在项目名下 是在tomcat下。。tomcat在c盘。。。项目在d:盘,。。。用的是netbeans
路径写的是。。/upload。。。。大家看看我该怎么改把东西才可以上传到。。项目名下的目录里
这个是代码。。。
=============================================================================
一个文件上传JAVABEAN


package com.upload;

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

public class upload{
private static String newline = "\n";
private String uploadDirectory = ".";
private String ContentType = "";
private String 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 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(String parent, String child)
//Creates a new File instance from a parent pathname string
//and a child pathname string.
File file = new File(uploadDirectory, filename);

//FileOutputStream(File file)
//Creates a file output stream to write to the file represented
//by the specified File object.
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){
//write(byte[] b, int off, int len)
//Writes len bytes from the specified byte array starting
//at offset off to this file output stream.
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();
}
}

private String readLine(byte Linebyte[], int ai[],
ServletInputStream servletinputstream,
String CharacterEncoding){

try{
//readLine(byte[] buffer, int offset, int length)
//Reads a line from the POST data.
ai[0] = servletinputstream.readLine(Linebyte, 0, Linebyte.length);
if(ai[0] == -1)
return null;
}catch(IOException _ex){
return null;
}
try{
if(CharacterEncoding == null){
//用缺省的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes, int offset, int length)
return new String(Linebyte, 0, ai[0]);
}else{
//用给定的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes, int offset, int length, String enc)
return new String(Linebyte, 0, ai[0], CharacterEncoding);
}
}catch(Exception _ex){
return null;
}
}
/*
public int readLine(byte[] buffer,
int offset,
int length) throws java.io.IOException
从POST来的数据中读一行
参数:
buffer - buffer to hold the line data
offset - offset into the buffer to start
length - maximum number of bytes to read.
Returns:
number of bytes read or -1 on the end of line.
*/
}


upload.jsp
<%@page import="com.upload.upload"%>
<%
String Dir = "。。/upload";

upload upload = new upload();
upload.setUploadDirectory(Dir);
upload.uploadFile(request);
out.print("<center><font color=red>成功上载文件至" + Dir + "</font></center>");
%>


upload.html
<center>File upload test</center>
<br>
<table><tr>
<form method="post" enctype="multipart/form-data" action="upload.jsp">
<td> <input type=file size=20 name="fname"> </td>
<td> <input type=Submit value=Upload> </td> </form>
</tr></table>

upload.setUploadDirectory(Dir); 就是设置存放目录


老牛明知夕阳晚,不用扬鞭自奋蹄; Angelo\'s BLOG
2007-01-04 16:39
快速回复:上传的问题
数据加载中...
 
   



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

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