java struts2上传文件(ajaxfileupload.js)

html部分代码:

<input type='file' size='20' style='height: 25px;width:60%;' name='file' id='corp_icon_id'>
<input type='button' οnclick="uploadFile('corp_icon_id')" value='上传'>

 

js部分代码:

function uploadFile(fileId) {
var fileIdValue = $("#" + fileId).val();
if (!fileIdValue) {
mini.alert('请选择要上传的文件!', '提示');
} else {
doSaveByfreshFlag(false);
jQuery.ajaxFileUpload({
url : '<%=basePath%>basicData/OperationUnits_uploadFile.action',
secureuri : false,
fileElementId : fileId,
dataType : 'text',
type : "POST",
data : {
"fileFullName":fileIdValue,
"tcorp.corp_id":corp_id
},
success : function(data, status) {
window.location.href ="basicData/OperationUnits_edit.action?primaryKey=" + corp_id +"&optype=edit";
},
error : function(data, status, e) {
mini.alert('上传失败!', '提示');
}
});
return false;
}
 
}

 

java部分代码:

private File file;//实现get和set方法
 
 
public String uploadFile() {
String fileFullName = this.getParameter("fileFullName");
String id = this.getParameter("id");
String corpId = this.getParameter("corp_id");
String suffixName = fileFullName.substring(fileFullName.lastIndexOf("."));
String showName = fileFullName.substring(fileFullName.lastIndexOf("\\") + 1);
 
String uploadRelativePath = "upload/images/";
String Serverpath = this.getServletContext().getRealPath("/");
String uploadFullPath = Serverpath + uploadRelativePath;
File tempFile = this.getFile();
File outFile = new File(uploadFullPath); // 判断文件夹是否存在,如果不存在则创建文件夹
if (!outFile.exists()) {
outFile.mkdirs();
}
UUIDHexGenerator t = new UUIDHexGenerator(); // 文件名不能重复
String saveRelativeFileName = uploadRelativePath + t.generate() + suffixName;
String saveFullFileName = Serverpath + saveRelativeFileName;
try {
FileInputStream inputStream = new FileInputStream(tempFile);
FileOutputStream outputStream = new FileOutputStream(saveFullFileName);
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
outputStream.close();
 
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
 
this.tcorp.setCorp_icon(saveRelativeFileName);
int resultCount = 0;
resultCount = this.basicDataService.updateTcorp(tcorp);
 
HashMap<String, String> resultMap = new HashMap<String, String>();
resultMap.put("count", resultCount + "");
String result = PluSoft.Utils.JSON.Encode(resultMap);
this.ajax(result);
return null;
}

 

 

 

简单的利用javajs实现文件上传 package com.fendou.myString; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class FileUpload extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean a=ServletFileUpload.isMultipartContent(request); if(a){ FileItemFactory factory=new DiskFileItemFactory(); ServletFileUpload upload=new ServletFileUpload (factory); Iterator items; try{ items=upload.parseRequest (request).iterator(); while(items.hasNext()){ FileItem item=(FileItem) items.next(); if(!item.isFormField()){ String name=item.getName (); String fileName=name.substring(name.lastIndexOf("\\")+1, name.length()); String path=request.getRealPath("file")+File.pathSeparatorChar+fileName; File uploadFile=new File (path); request.getSession ().setAttribute("file", uploadFile); item.write(uploadFile); response.setContentType ("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); // out.print("<font size='2'> 上传的文件为:"+name+"<br>"); // out.print("保存在服务器上 的地址为:"+path+"</font>"); } } }catch(Exception e){ e.printStackTrace(); } } response.sendRedirect("smartupload.jsp"); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值