/**
* 利用struts的FormFile上传图片
* @param mapping
* @param actionForm
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward upload(ActionMapping mapping, ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response) throws Exception {
AdvertiseForm advertiseForm = (AdvertiseForm) actionForm;
FormFile file=advertiseForm.getFormFile("picFile");
try{
String filename=FileBean.uploadFile(this.getServlet(),IConstants.UPLOAD_PATH, file, request,true);
System.out.println("上次的文件名称为:"+filename);
response.getWriter().print(filename);
}catch (Exception e) {
e.printStackTrace();
response.getWriter().print("上传失败!");
}
return null;
}
private FormFile picFile;//actionForm里面要设置
public static String uploadFile(HttpServlet servlet,String path,FormFile file, HttpServletRequest request,boolean flag){
String fileName=getExtention(file.getFileName());
if(file==null){
return "";
}
if(flag){
// String webapps=request.getRealPath("/");
// webapps=webapps.substring(0,webapps.lastIndexOf("webapps")+"webapps".length());
// path=webapps+path;
path = request.getRealPath("/")+path;
}
else path=request.getRealPath("/")+"/resentFiles";
InputStream streamin = null;
OutputStream bos = null;
try{
File filepath = new File(path);
if (!filepath.exists()) {
filepath.mkdirs();
}
fileName=GUID.getInstance().get16()+fileName;
streamin = file.getInputStream();
bos = new FileOutputStream(path +"/"+fileName);
int bytesRead= 0;
byte[] sum = new byte[8192];
int i=0;
while ((bytesRead = streamin.read(sum, 0, 8192)) != -1)
{
i=i+bytesRead;
bos.write(sum, 0, bytesRead);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(bos!=null){
bos.close();
}
if(streamin!=null){
streamin.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return fileName;
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
if(pos<1){
return null;
}
return fileName.substring(pos,fileName.length());
}
分享到:
相关推荐
struts 单文件上传和多文件上传带进度条
struts formFile 批量上传图片 源代码
struts2文件上传与下载, <s:fielderror></s:fielderror> 文件"></s:file> <s:submit id="submit" value="sbumit"></s:submit> </s:form>
解决struts组件上传附件速度很慢的问题
下面小编就为大家带来一篇基于Struts文件上传(FormFile)详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
早前我做JSP项目的时候,用过jspSmartUpload这个组件来上传和下载图片,但是现在做的是SSH整合的项目,而struts又提供了一个FormFile类来做文件的上传和下载,如果还用jspSmartUpload来做的话,有点说不过去,于是试...
用struts制作的文件上传然后显示出来的小项目,主要运用了FormFile
struts文件上传,struts upload组件文件上传 最基本的实例,将文件上传到应用服务器里 1.upload.jsp内容 <form action="fileup.do" method="post" enctype="multipart/form-data" > 文件:...
Struts的文件上传 本文用的是Struts的org.apache.struts.upload.FormFile类。很方便,不用自己写。也不用写一个jsp调用jspsmartupload就可以搞定。
ssh框架集成,批量上传文件,简单易理解,使用struts自带jar包。
* 使用org.apache.strtus.upload.FormFile实现文件的单一上传 */ private void singleUploadFile(IndexActionForm objForm) { if(objForm.getFile().getFileSize() == 0|| BUFFER_SIZE ().getFileSize()...
java 文件上传 文件下载 Struts项目中提供FormFile类来处理文件上传
环境为java,struts2,extjs...没有完全实现的是extjs用uploadfild控件实现的blob数据上传预览,并且尝试了用formFile上传文件、和smart组件上传,但两者在我的环境里还有问题,大家可以改进一下,本人觉得有总比没有强哈
2)、新建FormFile类,用来封装文件信息 package com.ljq.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; /** * 上传文
//上传文件的名称 private String fileSize;//上传文件的大小 private String filePath;//上传文件到服务器的路径 private String fileDate;//上传文件的日期 private FormFile file;//上传文件 public String...
其中save(FileActionForm fileForm)方法,将封装在fileForm中的上传文件保存到数据库中,这里我们使用FileActionForm作为方法入参,FileActionForm是Web层的表单数据对象,它封装了提交表单的数据。将...