当前位置: 技术问答>java相关
java中如何实现基于c/s的文件上载??做个相关工作的朋友来帮个忙吧
来源: 互联网 发布时间:2015-10-20
本文导语: 目前网上有几个上载文件的例子,但好象是给jsp用的,是基于b/s的模式,或则是ftp形式的。 现我要采用client/server模式,客户端与服务器建立连接时,从服务器端获取ip,port和存放目录,client端就可将目标文件上载到服...
目前网上有几个上载文件的例子,但好象是给jsp用的,是基于b/s的模式,或则是ftp形式的。
现我要采用client/server模式,客户端与服务器建立连接时,从服务器端获取ip,port和存放目录,client端就可将目标文件上载到服务器指定的存放路径中。
如何实现上面的功能?
有朋友写过这样的程序吗?
帮个忙吧
谢了先!
现我要采用client/server模式,客户端与服务器建立连接时,从服务器端获取ip,port和存放目录,client端就可将目标文件上载到服务器指定的存放路径中。
如何实现上面的功能?
有朋友写过这样的程序吗?
帮个忙吧
谢了先!
|
好长啊
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletException;
public class UploadFileServlet extends HttpServlet {
private ServletInputStream sin = null;
private OutputStream fout = null;
private String CharacterEncoding = "";
private String ContentType = "";
private String filename = "";
private byte temp[] = new byte[4096];
protected static final String newline = "n";
protected static final String UploadDir = "./config/mydomain/applications/DefaultWebApp";
protected final void doPost(HttpServletRequest request, HttpServletResponse response) {
System.out.println("************************************");
upload(request, response);
}
protected final void doGet(HttpServletRequest request, HttpServletResponse response) {
System.out.println("************************************");
System.out.println("request.getMethod():" + request.getMethod());
// upload(request, response);
System.out.println(request.getMethod() + " CAN NOT UPLOAD A FILE");
}
public void upload(HttpServletRequest request, HttpServletResponse response) {
String ContentType = "";
int i = 0;
setCharacterEncoding(request.getCharacterEncoding());
System.out.println("request.getMethod():" + request.getMethod());
System.out.println("request.getContentType():" + request.getContentType());
System.out.println("ok");
setContentType(request.getContentType());
try {
sin = request.getInputStream();
filename = getFileName(sin);
//**************
i = sin.readLine(temp, 0, temp.length);
if (this.CharacterEncoding != null) {
ContentType = new String(temp, 0, i, this.CharacterEncoding);
}
else {
ContentType = new String(temp, 0, i);
}
System.out.println("ContentType:" + ContentType);
if (ContentType.indexOf("Content-Type") >= 0) {
System.out.println("uploading ...");
sin.readLine(temp, 0, temp.length);
}
//**************
if ((filename != null) || (!filename.equals(""))) {
fout = new FileOutputStream(new File(UploadDir, filename));
while ((i = sin.readLine(temp, 0, temp.length)) != -1) {
if (this.CharacterEncoding != null) {
ContentType = new String(temp, 0, i, this.CharacterEncoding);
}
else {
ContentType = new String(temp, 0, i);
}
if ((ContentType.indexOf(this.ContentType) == 0)&&(temp[0] == 45)) {
System.out.println("this.ContentType:" + this.ContentType);
break;
}
fout.write(temp,0,i);
// fout.write(newline.getBytes());
}
}
}
catch (IOException ioe) {
System.out.print(ioe.getMessage());
}
finally {
try {
fout.close();
}
catch (Exception e) {
}
}
System.out.println("***** FINISHED ******");
}
//request.getContentType()·½·¨µÃµ½String s
protected void setContentType(String s) {
ContentType = s;
int j;
System.out.println("ContentType.indexOf("boundary="):" + ContentType.indexOf("boundary="));
if((j = ContentType.indexOf("boundary=")) != -1) {
ContentType = ContentType.substring(j + 9);
ContentType = "--" + ContentType;
}
System.out.println("ContentType: " + ContentType);
//it is commonly null
}
//request.getCharacterEncoding()·½·¨µÃµ½String s
protected void setCharacterEncoding(String s) {
CharacterEncoding = s;
System.out.println("CharacterEncoding: " + s);
}
//get filename
private String getFileName(ServletInputStream sin) {
String filename = "";
String str = "";
int i = 0;
int j = 0;
try {
while ((i = sin.readLine(temp, 0, temp.length)) != -1) {
if (this.CharacterEncoding != null) {
str = new String(temp, 0, i, this.CharacterEncoding);
}
else {
str = new String(temp, 0, i);
}
if ((j = str.indexOf("filename=")) != -1) {
j += 10;
str = str.substring(j);
System.out.println("str:" + str);
// the last string of "
if ((j = str.indexOf(""")) > 0) {
str = str.substring(0, j);
}
//now str is the file directory in the client pc
if ((j = str.lastIndexOf("\")) != -1) {
filename = str.substring(j);
}
break;
}
}
}
catch (IOException ioe) {
System.out.println("ERROR");
System.out.println(ioe.getMessage());
}
System.out.println("filename:" + filename + ".");
return filename;
}
}
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletException;
public class UploadFileServlet extends HttpServlet {
private ServletInputStream sin = null;
private OutputStream fout = null;
private String CharacterEncoding = "";
private String ContentType = "";
private String filename = "";
private byte temp[] = new byte[4096];
protected static final String newline = "n";
protected static final String UploadDir = "./config/mydomain/applications/DefaultWebApp";
protected final void doPost(HttpServletRequest request, HttpServletResponse response) {
System.out.println("************************************");
upload(request, response);
}
protected final void doGet(HttpServletRequest request, HttpServletResponse response) {
System.out.println("************************************");
System.out.println("request.getMethod():" + request.getMethod());
// upload(request, response);
System.out.println(request.getMethod() + " CAN NOT UPLOAD A FILE");
}
public void upload(HttpServletRequest request, HttpServletResponse response) {
String ContentType = "";
int i = 0;
setCharacterEncoding(request.getCharacterEncoding());
System.out.println("request.getMethod():" + request.getMethod());
System.out.println("request.getContentType():" + request.getContentType());
System.out.println("ok");
setContentType(request.getContentType());
try {
sin = request.getInputStream();
filename = getFileName(sin);
//**************
i = sin.readLine(temp, 0, temp.length);
if (this.CharacterEncoding != null) {
ContentType = new String(temp, 0, i, this.CharacterEncoding);
}
else {
ContentType = new String(temp, 0, i);
}
System.out.println("ContentType:" + ContentType);
if (ContentType.indexOf("Content-Type") >= 0) {
System.out.println("uploading ...");
sin.readLine(temp, 0, temp.length);
}
//**************
if ((filename != null) || (!filename.equals(""))) {
fout = new FileOutputStream(new File(UploadDir, filename));
while ((i = sin.readLine(temp, 0, temp.length)) != -1) {
if (this.CharacterEncoding != null) {
ContentType = new String(temp, 0, i, this.CharacterEncoding);
}
else {
ContentType = new String(temp, 0, i);
}
if ((ContentType.indexOf(this.ContentType) == 0)&&(temp[0] == 45)) {
System.out.println("this.ContentType:" + this.ContentType);
break;
}
fout.write(temp,0,i);
// fout.write(newline.getBytes());
}
}
}
catch (IOException ioe) {
System.out.print(ioe.getMessage());
}
finally {
try {
fout.close();
}
catch (Exception e) {
}
}
System.out.println("***** FINISHED ******");
}
//request.getContentType()·½·¨µÃµ½String s
protected void setContentType(String s) {
ContentType = s;
int j;
System.out.println("ContentType.indexOf("boundary="):" + ContentType.indexOf("boundary="));
if((j = ContentType.indexOf("boundary=")) != -1) {
ContentType = ContentType.substring(j + 9);
ContentType = "--" + ContentType;
}
System.out.println("ContentType: " + ContentType);
//it is commonly null
}
//request.getCharacterEncoding()·½·¨µÃµ½String s
protected void setCharacterEncoding(String s) {
CharacterEncoding = s;
System.out.println("CharacterEncoding: " + s);
}
//get filename
private String getFileName(ServletInputStream sin) {
String filename = "";
String str = "";
int i = 0;
int j = 0;
try {
while ((i = sin.readLine(temp, 0, temp.length)) != -1) {
if (this.CharacterEncoding != null) {
str = new String(temp, 0, i, this.CharacterEncoding);
}
else {
str = new String(temp, 0, i);
}
if ((j = str.indexOf("filename=")) != -1) {
j += 10;
str = str.substring(j);
System.out.println("str:" + str);
// the last string of "
if ((j = str.indexOf(""")) > 0) {
str = str.substring(0, j);
}
//now str is the file directory in the client pc
if ((j = str.lastIndexOf("\")) != -1) {
filename = str.substring(j);
}
break;
}
}
}
catch (IOException ioe) {
System.out.println("ERROR");
System.out.println(ioe.getMessage());
}
System.out.println("filename:" + filename + ".");
return filename;
}
}
|
/**SiteFileFetch.java*/
package NetFox;
import java.io.*;
import java.net.*;
public class SiteFileFetch extends Thread {
SiteInfoBean siteInfoBean = null; //文件信息Bean
long[] nStartPos; //开始位置
long[] nEndPos; //结束位置
FileSplitterFetch[] fileSplitterFetch; //子线程对象
long nFileLength; //文件长度
boolean bFirst = true; //是否第一次取文件
boolean bStop = false; //停止标志
File tmpFile; //文件下载的临时信息
DataOutputStream output; //输出到文件的输出流
public SiteFileFetch(SiteInfoBean bean) throws IOException{
siteInfoBean = bean;
//tmpFile = File.createTempFile ("zhong","1111",new File(bean.getSFilePath()));
tmpFile = new File(bean.getSFilePath()+File.separator + bean.getSFileName()+".info");
if(tmpFile.exists ()){
bFirst = false;
read_nPos();
}else{
nStartPos = new long[bean.getNSplitter()];
nEndPos = new long[bean.getNSplitter()];
}
}
public void run(){
//获得文件长度
//分割文件
//实例FileSplitterFetch
//启动FileSplitterFetch线程
//等待子线程返回
try{
if(bFirst){
nFileLength = getFileSize();
if(nFileLength == -1){
System.err.println("File Length is not known!");
}else if(nFileLength == -2){
System.err.println("File is not access!");
}else{
for(int i=0;i
package NetFox;
import java.io.*;
import java.net.*;
public class SiteFileFetch extends Thread {
SiteInfoBean siteInfoBean = null; //文件信息Bean
long[] nStartPos; //开始位置
long[] nEndPos; //结束位置
FileSplitterFetch[] fileSplitterFetch; //子线程对象
long nFileLength; //文件长度
boolean bFirst = true; //是否第一次取文件
boolean bStop = false; //停止标志
File tmpFile; //文件下载的临时信息
DataOutputStream output; //输出到文件的输出流
public SiteFileFetch(SiteInfoBean bean) throws IOException{
siteInfoBean = bean;
//tmpFile = File.createTempFile ("zhong","1111",new File(bean.getSFilePath()));
tmpFile = new File(bean.getSFilePath()+File.separator + bean.getSFileName()+".info");
if(tmpFile.exists ()){
bFirst = false;
read_nPos();
}else{
nStartPos = new long[bean.getNSplitter()];
nEndPos = new long[bean.getNSplitter()];
}
}
public void run(){
//获得文件长度
//分割文件
//实例FileSplitterFetch
//启动FileSplitterFetch线程
//等待子线程返回
try{
if(bFirst){
nFileLength = getFileSize();
if(nFileLength == -1){
System.err.println("File Length is not known!");
}else if(nFileLength == -2){
System.err.println("File is not access!");
}else{
for(int i=0;i