java文件上传客户端和服务器端Word格式文档下载.docx
《java文件上传客户端和服务器端Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《java文件上传客户端和服务器端Word格式文档下载.docx(14页珍藏版)》请在冰豆网上搜索。
text/html;
charset=UTF-8"
//设置Content-Type字段值
PrintWriterout=response.getWriter();
out.println("
文件开始!
"
//下面的代码开始使用Commons-UploadFile组件处理上传的文件数据
FileItemFactoryfactory=newDiskFileItemFactory();
//建立FileItemFactory对象
ServletFileUploadupload=newServletFileUpload(factory);
//分析请求,并得到上传文件的FileItem对象
List<
FileItem>
items=upload.parseRequest(request);
//从web.xml文件中的参数中得到上传文件的路径
StringuploadPath="
d:
\\upload\\"
;
Filefile=newFile(uploadPath);
if(!
file.exists())
file.mkdir();
}
Stringfilename="
//上传文件保存到服务器的文件名
InputStreamis=null;
//当前上传文件的InputStream对象
//循环处理上传文件
for(FileItemitem:
items)
//处理普通的表单域
if(item.isFormField())
{
if(item.getFieldName().equals("
filename"
))
{
//如果新文件不为空,将其保存在filename中
if(!
item.getString().equals("
filename=item.getString("
}
}
//处理上传文件
elseif(item.getName()!
=null&
&
!
item.getName().equals("
//从客户端发送过来的上传文件路径中截取文件名
filename=item.getName().substring(
item.getName().lastIndexOf("
\\"
)+1);
is=item.getInputStream();
//得到上传文件的InputStream对象
//将路径和上传文件名组合成完整的服务端路径
filename=uploadPath+BOUNDARY+filename;
//如果服务器已经存在和上传文件同名的文件,则输出提示信息
if(newFile(filename).exists())
newFile(filename).delete();
//开始上传文件
filename.equals("
//用FileOutputStream打开服务端的上传文件
FileOutputStreamfos=newFileOutputStream(filename);
byte[]buffer=newbyte[8192];
//每次读8K字节
intcount=0;
//开始读取上传文件的字节,并将其输出到服务端的上传文件输出流中
while((count=is.read(buffer))>
0)
fos.write(buffer,0,count);
//向服务端文件写入字节流
fos.close();
//关闭FileOutputStream对象
is.close();
//InputStream对象
out.println("
文件上传成功!
}
catch(Exceptione)
}}
packagecom.gem.hsx.activity;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.Map;
importandroid.app.ListActivity;
importandroid.content.Context;
importandroid.content.Intent;
import.Uri;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.Display;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.view.WindowManager;
importandroid.view.WindowManager.LayoutParams;
importandroid.widget.BaseAdapter;
importandroid.widget.ImageView;
importandroid.widget.ListView;
importandroid.widget.TextView;
importcom.gem.hsx.activity.R;
publicclassExDialogextendsListActivity{
privateList<
Map<
String,Object>
>
mData;
privateStringmDir="
/sdcard"
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
Intentintent=this.getIntent();
Bundlebl=intent.getExtras();
Stringtitle=bl.getString("
explorer_title"
Uriuri=intent.getData();
mDir=uri.getPath();
setTitle(title);
mData=getData();
MyAdapteradapter=newMyAdapter(this);
setListAdapter(adapter);
WindowManagerm=getWindowManager();
Displayd=m.getDefaultDisplay();
LayoutParamsp=getWindow().getAttributes();
p.height=(int)(d.getHeight()*0.8);
p.width=(int)(d.getWidth()*0.95);
getWindow().setAttributes(p);
}
getData(){
List<
list=newArrayList<
();
Map<
map=null;
Filef=newFile(mDir);
File[]files=f.listFiles();
if(!
mDir.equals("
)){
map=newHashMap<
map.put("
title"
"
返回上一级目录/"
info"
f.getParent());
img"
R.drawable.ex_folder);
list.add(map);
if(files!
=null){
for(inti=0;
i<
files.length;
i++){
map=newHashMap<
map.put("
files[i].getName());
files[i].getPath());
if(files[i].isDirectory())
map.put("
else
R.drawable.ex_doc);
list.add(map);
returnlist;
protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid){
Log.d("
MyListView4-click"
(String)mData.get(position).get("
));
if((Integer)mData.get(position).get("
)==R.drawable.ex_folder){
mDir=(String)mData.get(position).get("
mData=getData();
MyAdapteradapter=newMyAdapter(this);
setListAdapter(adapter);
}else{
finishWithResult((String)mData.get(position).get("
publicfinalclassViewHolder{
publicImageViewimg;
publicTextViewtitle;
publicTextViewinfo;
publicclassMyAdapterextendsBaseAdapter{
privateLayoutInflatermInflater;
publicMyAdapter(Contextcontext){
this.mInflater=LayoutInflater.from(context);
publicintgetCount(){
returnmData.size();
publicObjectgetItem(intarg0){
returnnull;
publiclonggetItemId(intarg0){
return0;
publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
ViewHolderholder=null;
if(convertView==null){
holder=newViewHolder();
convertView=mInflater.inflate(R.layout.listview,null);
holder.img=(ImageView)convertView.findViewById(R.id.img);
holder.title=(TextView)convertView.findViewById(R.id.title);
//holder.info=(TextView)convertView.findViewById(R.id.info);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
holder.img.setBackgroundResource((Integer)mData.get(position).get(
"
holder.title.setText((String)mData.get(position).get("
//holder.info.setText((String)mData.get(position).get("
returnconvertView;
privatevoidfinishWithResult(Stringpath){
BundleconData=newBundle();
conData.putString("
results"
ThanksThanks"
Intentintent=newIntent();
intent.putExtras(conData);
UristartDir=Uri.fromFile(newFile(path));
intent.setDataAndType(startDir,
"
vnd.android.cursor.dir/lysesoft.andexplorer.file"
setResult(RESULT_OK,intent);
finish();
};
importcom.gem.hsx.util.UploadUtil;
importandroid.app.Activity;
importandroid.app.ProgressDialog;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
publicclassUpFile3ActivityextendsActivity{
privatestaticfinalintREQUEST_EX=1;
privateStringrequestURL="
http:
//10.110.5.38:
8080/UpFile1/UploadServlet"
privateButtonup;
Stringfilepath;
ProgressDialogp;
publicvoidonCreate(BundlesavedInstanceState){
setContentView(R.layout.main);
Buttonbutton=(Button)findViewById(R.id.button);
up=(Button)findViewById(R.id.up);
button.setOnClickListener(newView.OnClickListener(){
publicvoidonClick(Viewv){
Intentintent=newIntent();
intent.putExtra("
getString(R.string.dialog_read_from_dir));
intent.setDataAndType(Uri.fromFile(newFile("
)),"
*/*"
intent.setClass(UpFile3Activity.this,ExDialog.class);
startActivityForResult(intent,REQUEST_EX);
});
up.setOnClickListener(newUpOnclick());
protectedvoidonActivityResult(intrequestCode,intresultCode,
Intentintent){
if(resultCode==RESULT_OK){
if(requestCode==REQUEST_EX){
Uriuri=intent.getData();
TextViewtext=(TextView)findViewById(R.id.text);
text.setText("
文件路径为"
+uri);
filepath=uri.toString().substring(6);
System.out.println(filepath);
privateclassUpOnclickimplementsOnClickListener
{
publicvoidonClick(Viewv){
p=newProgressDialog(UpFile3Activity.this);
p.setTitle("
上传中"
p.setMessage("
正在上传,请等待"
p.show();
newThread(newRunnable(){
publicvoidrun(){
Filefile=newFile(filepath);
UploadUtiluploadUtil=newUploadUtil();
uploadUtil.uploadFile(file,requestURL);
Messagemessage=newMessage();
message.obj="
上传完成"
handler.sendMessage(message);
}).start();
Handlerhandler=newHandler()
@Override
publicvoidhandleMessage(Messagemsg){
System.out.println(msg.toString());
p.dismiss();
super.handleMessage(msg);
};
}
packagecom.gem.hsx.util;
importjava.io.DataOutputStream;
importjava.io.FileInputStream;
import.HttpURLConnect