java????url???????????????????struts2??action???????
???????????? ???????[ 2013/7/2 10:42:39 ] ????????
????3??????????????struts2???????????????????ajaxUploadFile??ajaxUploadFileTwo????action?????????
package com.eshopmates.finance.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
/**
* @???? ??????
* @???????? 2013-06-27
* @??????? 18:22:33
* @?汾?? V 1.0
*/
public class GetPostFileAction extends BaseAction {
// ??????
private File upFile;// ????????????????????????????????????????????
private String upFileContentType;// ????е??
private String upFileFileName;// ???????е?name
/**
* @return
* @???? ??????
* @???????? 2013-06-27
* @??????? 19:26:22
* @???? —— ???????????????????????file??????д???
*/
@org.apache.struts2.convention.annotation.Action("ajaxUploadFile")
public String ajaxUploadFile() {
String result;
try {
String path = getRequest().getSession().getServletContext()
.getRealPath("/uploadFile/" + upFileFileName);// ·??
File currFile = new File(path);
System.out
.println("?????????????·??======>" + currFile.getAbsolutePath());
FileUtils.copyFile(this.upFile?? currFile);// struts2??????????????????????????????
result = "{"success":true??"uploadFile":"" + currFile.getAbsolutePath()
+ ""??"fileSize":" + currFile.length() + "}";
} catch (IOException e) {
e.printStackTrace();
result = "{"success":false}";
}
super.ajaxPrintMsg(result?? super.CONTENTTYPE_HTML);
System.out.println("result========>" + result);
return Action.NONE;
}
/**
* @return
* @throws Exception
* @???? ??????
* @???????? 2013-06-27
* @??????? 19:26:55
* @???? —— ????????????????????????post??
*/
@org.apache.struts2.convention.annotation.Action("ajaxUploadFileTwo")
public String ajaxUploadFileTwo() throws Exception {
String result;
HttpServletRequest request = ServletActionContext.getRequest();
String rootPath = request.getSession().getServletContext().getRealPath(
"/");
String filePath = request.getParameter("filePath");
String fileName = request.getParameter("fileName");
System.out.println("fileName=====>" + fileName);
InputStream input = request.getInputStream();
String fileFullPath = rootPath + filePath + fileName;
File saveFile = new File(fileFullPath);
File file = new File(rootPath + filePath);
if (!file.exists()) {
file.mkdirs();
}
FileOutputStream fos = new FileOutputStream(fileFullPath);
int size = 0;
byte[] buffer = new byte[1024];
while ((size = input.read(buffer?? 0?? 1024)) != -1) {
fos.write(buffer?? 0?? size);
}
fos.close();
input.close();
result = "{"success":true??"uploadFileName":""
+ saveFile.getAbsolutePath() + ""??"fileSize":"
+ saveFile.length() + "}";
super.ajaxPrintMsg(result?? super.CONTENTTYPE_HTML);
System.out.println("filePath===>" + file.getAbsolutePath());
return Action.NONE;
}
public File getUpFile() {
return upFile;
}
public void setUpFile(File upFile) {
this.upFile = upFile;
}
public String getUpFileContentType() {
return upFileContentType;
}
public void setUpFileContentType(String upFileContentType) {
this.upFileContentType = upFileContentType;
}
public String getUpFileFileName() {
return upFileFileName;
}
public void setUpFileFileName(String upFileFileName) {
this.upFileFileName = upFileFileName;
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11