webwork下uplodify的使用步骤:
1.修改web.xml文件中的拦截器配置,如下:
2.在类路径下的webwork.properties中修改有关上传功能的配置,如下:
### multipart setting
webwork.tag.altSyntax = true
webwork.multipart.saveDir=javax.servlet.context.tempdir
webwork.multipart.maxSize=2097152
webwork.multipart.parser=jakarta
3.新建以xwork_upload.xml配置文件,文件内容如下:
10000000
4.将刚刚新建的xwork_upload.xml配置文件引入到xwork.xml配置文件中
5.根据配置文件新建net.survey.admin.UploadAction,内容如下:
public class UploadAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{
private HttpServletRequest request;
private HttpServletResponse response;
private File images; //和在JS中指定的fileObjName的值相同
private String imagesFileName; //[fileName]FileName 获得上传文件的名称
private String imagesContentType;//[fileName]ContentType 获得上传文件的类型
public String doImageUpload() throws Exception {
upload("img", imagesFileName, images);
return null;
}
public void upload(String dir, String name, File file) throws Exception{
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String savePath = new String(request.getServletContext().getRealPath("/") + "\\upload\\" + dir);
if(!new File(savePath).exists())new File(savePath).mkdirs();
String file_name = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss").format(new Date()) + "_" + new String(name.getBytes("gbk"), "UTF-8"); // 唯一且不重复的文件名
//文件名形如:ID.文件后缀
FileOutputStream fos = new FileOutputStream(savePath + "\\" + file_name);
FileInputStream fis = new FileInputStream(file);
Streams.copy(fis, fos, true);
/*byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
fis.close();*/
PrintWriter out = response.getWriter();
//这个地方不能少,否则前台得不到上传的结果
out.write("1");
out.close();
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public File getImages() {
return images;
}
public void setImages(File images) {
this.images = images;
}
public String getImagesFileName() {
return imagesFileName;
}
public void setImagesFileName(String imagesFileName) {
this.imagesFileName = imagesFileName;
}
public String getImagesContentType() {
return imagesContentType;
}
public void setImagesContentType(String imagesContentType) {
this.imagesContentType = imagesContentType;
}
}
6.建立jsp页面,内容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
说明:该附件中的uploadify插件已经修复了buttonText不支持中文这一bug。