日期:2014-05-18  浏览次数:20904 次

struts中获取上传文件的大小
我在做上传文件.我想在点击浏览的时候可以获取文件的大小.请问如何实现.
急急急急急急急急急急急急急急急急急急急急急急急急急急

------解决方案--------------------
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package demo;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/**
* MyEclipse Struts Creation date: 07-02-2007
*
* XDoclet definition:
*
* @struts.action validate= "true "
*/
public class UploadAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 获取UploadBeanForm
UploadBeanForm uploadBeanForm = (UploadBeanForm) form;
// 从UploadBeanForm里面获取用户要上传的文件
FormFile file = uploadBeanForm.getFile();
if (file == null) {
return mapping.findForward( " ");
}

// 获取文件相关信息
String fileName = file.getFileName();
uploadBeanForm.setFilename(fileName);
String size = Integer.toString(file.getFileSize());
uploadBeanForm.setSize(size);

// 获取文件的输入流
InputStream is = file.getInputStream();
// 获取文件在服务器上的存储路径。
String storePath = servlet.getServletContext().getRealPath(
"/fileupload ");
/* storePath为绝对路径 */
// String storePath = "F:\\fileupload ";
// 获取文件的输出流
OutputStream os = new FileOutputStream(storePath + "/ " + fileName);
// 开始上传文件
int bytes = 0;
byte[] buffer = new byte[8192];
while ((bytes = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, 8192);
}
// 关闭输出流
os.close();
// 关闭输入流
is.close();
// 释放文件连接
file.destroy();
return mapping.findForward( "fileUpload ");
}
}
------解决方案--------------------
html可以的,在jsp中也行
------解决方案--------------------
jsp和html是一样的。你的那个用js实现的肯定也可以在jsp中用的。