我需要将上传的文件及其原始名称和类型保存在单独的文件夹中而不是临时文件名。
通过使用以下代码,我可以获取文件名和类型,但如何更改临时名称及其原始名称。
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";
}
通过此代码,可以使用临时文件名保存视频,但我希望使用其原始名称和类型保存文件....
或者可以在保存到文件夹
之前用原始名称重命名临时文件答案 0 :(得分:0)
打开
function onlineCheck() {
var y = document.querySelector('[title="online"]');
var d = new Date();
if (y == null) {
onlineCheck();
} else {
if (y.innerText === 'online') {
new Notification("contact is online");
console.log(d.toLocaleDateString() + "|" + addZero(d.getHours()) + ":" + addZero(d.getMinutes()) + ":" + addZero(d.getSeconds()) + " " + "Notification sent");
}
}
}
到
File dest = new File("/home/desktop/images/");
/* ... */
FileUtils.copyFileToDirectory(file1, dest);
答案 1 :(得分:0)
尝试下面的代码。
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>