如何在上传文件时在Struts2中保存文件的原始名称
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在上传文件时在Struts2中保存文件的原始名称相关的知识,希望对你有一定的参考价值。
我需要保存上传的文件及其原始名称和类型而不是临时文件名在单独的文件夹中。
通过使用下面的代码,我可以获取文件名和类型,但如何更改临时名称的原始名称。
private File file1;
private String file1ContentType;
private String file1FileName;
public File getFile1() {
return file1;
}
public void setFile1(File file1) {
this.file1 = file1;
}
public String getFile1ContentType() {
return file1ContentType;
}
public void setFile1ContentType(String file1ContentType) {
this.file1ContentType = file1ContentType;
}
public String getFile1FileName() {
return file1FileName;
}
public void setFile1FileName(String file1FileName) {
this.file1FileName = file1FileName;
}
public String upld() {
File dest = new File("/home/desktop/images/");
try {
System.out.println("file1FileName........"+file1FileName);
System.out.println("file1ContentType........"+file1ContentType);
System.out.println("file1........"+file1);
FileUtils.copyFileToDirectory(file1, dest);
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
通过此代码可以保存带有临时文件名的视频,但我想保存文件的原始名称和类型....
或者是否可以在保存到文件夹之前用其原始名称重命名临时文件
答案
转
File dest = new File("/home/desktop/images/");
/* ... */
FileUtils.copyFileToDirectory(file1, dest);
成
File dest = new File("/home/desktop/images/" + file1FileName);
/* ... */
FileUtils.copyFile(file1, dest);
另一答案
试试下面的代码。
jsp代码:
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<s:head />
</head>
<body>
<h1>Struts 2 file upload example</h1>
<s:form action="resultAction" namespace="/"
method="POST" enctype="multipart/form-data">
<s:file name="upload" label="Select a File to upload" size="40" />
User file : <s:property value="uploadFileName"></s:property>
<s:submit value="submit" name="submit" />
</s:form>
</body>
</html>
Java代码:
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
public class FileUploadAction extends ActionSupport implements ServletRequestAware {
private File upload;
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadDocContentType() {
return uploadDocContentType;
}
public void setUploadDocContentType(String uploadDocContentType) {
this.uploadDocContentType = uploadDocContentType;
}
private String uploadDocContentType;
public HttpServletRequest request;
public HttpSession session;
public String uploadFileName;
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String uploadImg() throws IOException{
String targetPath ="/Desination path/";
session = request.getSession(true);
System.out.println("targetPath->"+uploadFileName);
File fileToCreate = new File(targetPath, uploadFileName);
try
{
FileUtils.copyFile(this.upload, fileToCreate);
}
catch (IOException e)
{
addActionError(e.getMessage());
}
return "success";
}
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
}
}
在struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.multipart.maxSize" value="20097152" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default ,json-default">
<action name="resultAction" class="FileUploadAction" method="uploadImg">
<interceptor-ref name="fileUpload">
<param name="maximumSize">1024000</param>
<param name="allowedTypes">image/png,image/gif,image/jpeg,image/pjpeg </param>
</interceptor-ref>
<interceptor-ref name="basicStack"/>
<result name="success">/index.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
以上是关于如何在上传文件时在Struts2中保存文件的原始名称的主要内容,如果未能解决你的问题,请参考以下文章