struts2怎么判断文件上传的类型 请问怎样去判断上传文件的类型啊!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 限制类型可以用struts2的拦截器如果要在代码中判断 用XXXXContentType或者看下FileUploadInterceptor这个类的源码 在Action中,加入一个属性字段,即在接收的文件名后面加上表单传过来的文件名+ContentType,如:private File uplade;//接收的文件private String uploadContentType;//接收的文件类型这样在执行的方法里可以拿出来了。 请问我该怎么来判断用户上传的是什么文件类型呢!!比如说我该怎样判断用户上传是以exe格式结尾的呢!!以下是我的代码:请高手们指点一下,谢谢!public class UploadAction extends ActionSupport { private List<File> file; private List<String> fileFileName; private List<String> fileContentType; public List<File> getFile() { return file; } public void setFile(List<File> file) { this.file = file; } public List<String> getFileFileName() { return fileFileName; } public void setFileFileName(List<String> fileFileName) { this.fileFileName = fileFileName; } public List<String> getFileContentType() { return fileContentType; } public void setFileContentType(List<String> fileContentType) { this.fileContentType = fileContentType; } @Override public String execute() throws Exception { for (int i = 0; i < file.size(); ++i) { InputStream in = new FileInputStream(file.get(i)); //注释中是上传到服务器项目中upload目录下 String root = ServletActionContext.getRequest().getRealPath("/upload"); //获得文件路径和上传文件名 File destFile = new File(root, this.getFileFileName().get(i)); // File destFile = new File("F:/uploads", this.getFileFileName().get(i)); OutputStream out = new FileOutputStream(destFile); byte[] buffer = new byte[1024]; while ((in.read(buffer)) != -1) { out.write(buffer); } in.close(); out.close(); } return SUCCESS; }} FileUpload里面应该有提供的方法,最简单的是字符串截取substring File类有获取文件类型的方法实在不行就字符串截取 谢谢各位了!!我搞定它了,以后有问题还请大家多多指教,我用substring来完成它的!如果传过来的文件以exe结尾的我就给出用户一个提示!!返回到上传页面,谢谢各位了!!! 参数如下设置 //上传的文件 private File upload; // 上传文件名 private String uploadFileName; // 上传文件类型 private String uploadContentType; 可以在struts.xml进行配制<action name="demoAction" class="com.DemoAction"> <result>success.jsp</result> <interceptor-ref name="fileUpload"> <param name="maximumSize">6000000</param> <param "allowedTypes">image/jpeg,image/jpg,image/pjpeg</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> ssh整合出错( Unable to instantiate Action, userAction, defined for 'user_addInput') 求助啊!!马上~~~~~~~~ JS文件难题求助! 问 一个 javascript 和 css的 应用 ftp上传下载中的乱码问题 急求课表查询系统源码 如何在jsp页上做一个选择文件夹的控件 JSP访问控制和JSP怎样调用方法? 用JSP如何实现诸如新浪网那样的新闻系统! getParameter与getString的区别 这么多技术怎么学啊 请问一个字符编码问题
如果要在代码中判断
用XXXXContentType或者看下FileUploadInterceptor这个类的源码
private String uploadContentType;//接收的文件类型这样在执行的方法里可以拿出来了。
public class UploadAction extends ActionSupport {
private List<File> file; private List<String> fileFileName; private List<String> fileContentType;
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
@Override
public String execute() throws Exception {
for (int i = 0; i < file.size(); ++i)
{
InputStream in = new FileInputStream(file.get(i));
//注释中是上传到服务器项目中upload目录下
String root = ServletActionContext.getRequest().getRealPath("/upload");
//获得文件路径和上传文件名
File destFile = new File(root, this.getFileFileName().get(i));
// File destFile = new File("F:/uploads", this.getFileFileName().get(i)); OutputStream out = new FileOutputStream(destFile); byte[] buffer = new byte[1024];
while ((in.read(buffer)) != -1)
{
out.write(buffer);
} in.close(); out.close();
}
return SUCCESS;
}
}
//上传的文件
private File upload;
// 上传文件名
private String uploadFileName;
// 上传文件类型
private String uploadContentType;
<action name="demoAction" class="com.DemoAction">
<result>success.jsp</result>
<interceptor-ref name="fileUpload">
<param name="maximumSize">6000000</param>
<param "allowedTypes">image/jpeg,image/jpg,image/pjpeg</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>