当前位置: 移动技术网 > IT编程>开发语言>Java > java文件上传(单文件 多文件)与删除

java文件上传(单文件 多文件)与删除

2019年07月22日  | 移动技术网IT编程  | 我要评论

话不多说,请看代码

/**
* 文件上传--单文件
* 
* @param request
* @param response
* @param path
* 文件存放路径(path为webapp\后面的内容)
* @return
*/
public final static string fileupload(httpservletrequest request,
httpservletresponse response, string path) {
multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
map<string, multipartfile> filemap = multipartrequest.getfilemap();
multipartfile mfile = null;
for (iterator<?> i = filemap.keyset().iterator(); i.hasnext();) {
object obj = i.next();
mfile = (multipartfile) filemap.get(obj);
}
string filepath = "";
simpledateformat sdf = new simpledateformat("yyyymmddhhmmsssss");
try {
// 得到上传的文件的文件名
string filename = mfile.getoriginalfilename();
// 获取文件后缀名
if (filename != null && !("").equals(filename)) {
string fileext = filename.substring(filename.lastindexof("."));
// 按时间格式重新生成文件名
string newfilename = sdf.format(new date())
+ (int) (math.random() * 100) + fileext;
filepath = path + "/" + newfilename;
// 得到上传服务器的物理路径
path = request.getsession().getservletcontext()
.getrealpath("\\" + path);
// 文件流写到服务器端
file savefile = new file(path, newfilename);
filecopyutils.copy(mfile.getbytes(), savefile);
}
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return filepath;
}
/**
* 文件上传--多文件
* 
* @param request
* @param response
* @param filepaths
* (fileinputid,webapp\后面的内容)
* @return
*/
public final static map<string, object> fileuploads(
httpservletrequest request, httpservletresponse response,
string path) {
multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
map<string, multipartfile> filemap = multipartrequest.getfilemap();
map<string, object> filepaths = new hashmap<string, object>();
// 得到上传服务器的物理路径
string fileurl = request.getsession().getservletcontext()
.getrealpath("\\" + path);
for (iterator<?> i = filemap.keyset().iterator(); i.hasnext();) {
object obj = i.next();
multipartfile mfile = (multipartfile) filemap.get(obj);
// 得到上传的文件的文件名
string filename = mfile.getoriginalfilename();
if (filename == "" || filename == null) {
continue;
}
simpledateformat sdf = new simpledateformat("yyyymmddhhmmsssss");
// 获取文件后缀名
string fileext = filename.substring(filename.lastindexof("."));
// 按时间格式重新生成文件名
string newfilename = sdf.format(new date())
+ (int) (math.random() * 100) + fileext;
string filepath = path + "/" + newfilename;
// 文件流写到服务器端
try {
filepaths.put(obj.tostring(), filepath);
file savefile = new file(fileurl, newfilename);
filecopyutils.copy(mfile.getbytes(), savefile);
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
return filepaths;
}
/**
* 删除文件,
* 
* @param request请求
* @param filepath文件路径
* (static/upload/...)
* @return
*/
public static boolean filedelete(httpservletrequest request, string filepath) {
string fileurl = request.getsession().getservletcontext()
.getrealpath("\\" + filepath);// 得到上传服务器的物理路径
file file = new file(fileurl);
filedelete(file);
return false;
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网