前台js 后台java 怎么上传文件
页面时html+js实现的。后台通过webservice实现现在要在页面实现文件上传功能,将本地文件上传到服务器上。js代码怎么写啊还有后台怎么处理啊...
页面时html+js实现的。后台通过webservice实现
现在要在页面实现文件上传功能,将本地文件上传到服务器上。js代码怎么写啊还有后台怎么处理啊 展开
现在要在页面实现文件上传功能,将本地文件上传到服务器上。js代码怎么写啊还有后台怎么处理啊 展开
4个回答
展开全部
前台部分代码:
<form name="buildform" id="buildform" action=" " enctype="multipart/form-data" method="post">
<table>
<tr>
<td colspan="" class="label">文件上传:</td>
<td class="label" align="left">
<div id="d">
<input type="file" name="fileurl" value="" />
</div>
</td>
</tr>
</table>
后台部分代码java:
// 创建一个文件上传的工厂实例
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置文件缓存地址和缓存大小
factory.setRepository(new File(request.getRealPath("/")));
factory.setSizeThreshold(1024 * 1024 * 20);
// 用上工厂实例创建一个上传文件对象
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("gbk");
String name = "";
String code = "";
String sredate = "";
String ssetdate = "";
String setworker = "";
String reworker = "";
String memo = "";
String contents = "";
Long redptid = null;
Long setdptid = null;
String fileurl = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date setdate = null;
Date revisedate = null;
// 处理页面传过来的表单项
int k = 0;
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
// 遍历所有的表单项
for (int i = 0; i < items.size(); i++) {
FileItem item = (FileItem) items.get(i);
// 如果这个表单是普通表单域
if (item.isFormField()) {
// 取得表单名
String formname = item.getFieldName();
if(formname.equals("name")){
name = item.getString("gbk");
}else
if(formname.equals("code")){
code = item.getString("gbk");
}else
if(formname.equals("revisedate")){
sredate = item.getString("gbk");
if(!sredate.equals("")){
revisedate = sdf.parse(sredate);
}
}else
if(formname.equals("setdate")){
ssetdate = item.getString("gbk");
if(!ssetdate.equals(""))
setdate = sdf.parse(ssetdate);
}else
if(formname.equals("redptid")){
String s_redptid = item.getString("gbk");
if(!s_redptid.equals("x"))
redptid = Long.parseLong(s_redptid);
}else
if(formname.equals("setdptid")){
String s_setdptid = item.getString("gbk");
if(!s_setdptid.equals("x"))
setdptid = Long.parseLong(s_setdptid);
}else
if(formname.equals("setworker")){
setworker = item.getString("gbk");
}
/*// 取得表单的value值
String formvalue = item.getString("gbk");*/
}
// 如果是文件域
else {
k++;
// 取得文件域的表单域名
String fieldName = item.getFieldName();
// 取得文件名
String fileName = item.getName();
// 取得文件类型
String contentType = item.getContentType();
// 对于上传文件的存放地址来建立一个输出流
FileOutputStream fos = new FileOutputStream(request
.getRealPath("/")+"uploadimg/" + imgName);
// 判断上传文件是否在缓存中
if (item.isInMemory()) {
// 得到上传文件输入流
InputStream is = item.getInputStream();
byte[] buffer = new byte[1024];
int len;
// 将上传文件写入服务器文件中
while ((len = is.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();
}
}
}
}
<form name="buildform" id="buildform" action=" " enctype="multipart/form-data" method="post">
<table>
<tr>
<td colspan="" class="label">文件上传:</td>
<td class="label" align="left">
<div id="d">
<input type="file" name="fileurl" value="" />
</div>
</td>
</tr>
</table>
后台部分代码java:
// 创建一个文件上传的工厂实例
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置文件缓存地址和缓存大小
factory.setRepository(new File(request.getRealPath("/")));
factory.setSizeThreshold(1024 * 1024 * 20);
// 用上工厂实例创建一个上传文件对象
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("gbk");
String name = "";
String code = "";
String sredate = "";
String ssetdate = "";
String setworker = "";
String reworker = "";
String memo = "";
String contents = "";
Long redptid = null;
Long setdptid = null;
String fileurl = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date setdate = null;
Date revisedate = null;
// 处理页面传过来的表单项
int k = 0;
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
// 遍历所有的表单项
for (int i = 0; i < items.size(); i++) {
FileItem item = (FileItem) items.get(i);
// 如果这个表单是普通表单域
if (item.isFormField()) {
// 取得表单名
String formname = item.getFieldName();
if(formname.equals("name")){
name = item.getString("gbk");
}else
if(formname.equals("code")){
code = item.getString("gbk");
}else
if(formname.equals("revisedate")){
sredate = item.getString("gbk");
if(!sredate.equals("")){
revisedate = sdf.parse(sredate);
}
}else
if(formname.equals("setdate")){
ssetdate = item.getString("gbk");
if(!ssetdate.equals(""))
setdate = sdf.parse(ssetdate);
}else
if(formname.equals("redptid")){
String s_redptid = item.getString("gbk");
if(!s_redptid.equals("x"))
redptid = Long.parseLong(s_redptid);
}else
if(formname.equals("setdptid")){
String s_setdptid = item.getString("gbk");
if(!s_setdptid.equals("x"))
setdptid = Long.parseLong(s_setdptid);
}else
if(formname.equals("setworker")){
setworker = item.getString("gbk");
}
/*// 取得表单的value值
String formvalue = item.getString("gbk");*/
}
// 如果是文件域
else {
k++;
// 取得文件域的表单域名
String fieldName = item.getFieldName();
// 取得文件名
String fileName = item.getName();
// 取得文件类型
String contentType = item.getContentType();
// 对于上传文件的存放地址来建立一个输出流
FileOutputStream fos = new FileOutputStream(request
.getRealPath("/")+"uploadimg/" + imgName);
// 判断上传文件是否在缓存中
if (item.isInMemory()) {
// 得到上传文件输入流
InputStream is = item.getInputStream();
byte[] buffer = new byte[1024];
int len;
// 将上传文件写入服务器文件中
while ((len = is.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();
}
}
}
}
展开全部
可以用Uploadify这个插件进行上传
参考网址:
http://www.uploadify.com/
http://www.blogjava.net/yangxiang/archive/2009/07/29/288888.html
前台绑定:
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : 'uploadify.swf',
'script' : 'servlet/Upload',
'cancelImg' : 'images/cancel.png',
'folder' : 'uploads',
'queueID' : 'fileQueue',
'auto' : false,
'multi' : true,
'simUploadLimit' : 2,
'buttonText' : 'BROWSE'
});
});
后台:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String savePath = this.getServletConfig().getServletContext()
.getRealPath("");
savePath = savePath + "/uploads/";
File f1 = new File(savePath);
System.out.println(savePath);
if (!f1.exists()) {
f1.mkdirs();
}
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("utf-8");
List fileList = null;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
return;
}
Iterator<FileItem> it = fileList.iterator();
String name = "";
String extName = "";
while (it.hasNext()) {
FileItem item = it.next();
if (!item.isFormField()) {
name = item.getName();
long size = item.getSize();
String type = item.getContentType();
System.out.println(size + " " + type);
if (name == null || name.trim().equals("")) {
continue;
}
//扩展名格式:
if (name.lastIndexOf(".") >= 0) {
extName = name.substring(name.lastIndexOf("."));
}
File file = null;
do {
//生成文件名:
name = UUID.randomUUID().toString();
file = new File(savePath + name + extName);
} while (file.exists());
File saveFile = new File(savePath + name + extName);
try {
item.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
response.getWriter().print(name + extName);
}
参考网址:
http://www.uploadify.com/
http://www.blogjava.net/yangxiang/archive/2009/07/29/288888.html
前台绑定:
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : 'uploadify.swf',
'script' : 'servlet/Upload',
'cancelImg' : 'images/cancel.png',
'folder' : 'uploads',
'queueID' : 'fileQueue',
'auto' : false,
'multi' : true,
'simUploadLimit' : 2,
'buttonText' : 'BROWSE'
});
});
后台:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String savePath = this.getServletConfig().getServletContext()
.getRealPath("");
savePath = savePath + "/uploads/";
File f1 = new File(savePath);
System.out.println(savePath);
if (!f1.exists()) {
f1.mkdirs();
}
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("utf-8");
List fileList = null;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
return;
}
Iterator<FileItem> it = fileList.iterator();
String name = "";
String extName = "";
while (it.hasNext()) {
FileItem item = it.next();
if (!item.isFormField()) {
name = item.getName();
long size = item.getSize();
String type = item.getContentType();
System.out.println(size + " " + type);
if (name == null || name.trim().equals("")) {
continue;
}
//扩展名格式:
if (name.lastIndexOf(".") >= 0) {
extName = name.substring(name.lastIndexOf("."));
}
File file = null;
do {
//生成文件名:
name = UUID.randomUUID().toString();
file = new File(savePath + name + extName);
} while (file.exists());
File saveFile = new File(savePath + name + extName);
try {
item.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
response.getWriter().print(name + extName);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
上传使用jquery的uploadfiy插件吧,很好很强大!
具体代码不好写,网上搜下相关资料,简单易学。
具体代码不好写,网上搜下相关资料,简单易学。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你上传文件需要远程调用?双方都是java的 可以使用rmi来实现 如果不是的话 还是套接字传好了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |