Struts2 Struts文件上传API的突然行为,当通过Struts API上传到服务器上时,文件会损坏
我有一个web项目,我正在使用Struts2框架。在这里人们上传pdf文件,为此我使用了struts文件上传API 但奇怪的是,服务器上每秒钟上传一次文件都会被破坏,当我试图打开文件时,会出现错误: 打开此文档时出错。文件已损坏,无法修复。 struts.xmlStruts2 Struts文件上传API的突然行为,当通过Struts API上传到服务器上时,文件会损坏,struts2,Struts2,我有一个web项目,我正在使用Struts2框架。在这里人们上传pdf文件,为此我使用了struts文件上传API 但奇怪的是,服务器上每秒钟上传一次文件都会被破坏,当我试图打开文件时,会出现错误: 打开此文档时出错。文件已损坏,无法修复。 struts.xml <action name="uploadFiles" class="org.webpannel.action.FileHandlingAction" method="saveFiles"> &
<action name="uploadFiles" class="org.webpannel.action.FileHandlingAction"
method="saveFiles">
<interceptor-ref name="customeUploadStack" />
<result name="success">/admin/WindowCloser.jsp</result>
<result name="input">/service/AttachmentPopup.jsp</result>
</action>
/admin/WindowCloser.jsp
/service/AttachmentPopup.jsp
文件处理操作:
public class FileHandlingAction extends BaseAction implements Preparable{
/**
*
*/
private static final long serialVersionUID = -470622101009022548L;
private File[] file;
private String[] contentType;
private String[] filename;
private List<String> attachmentsList= new ArrayList<String>();
private String directoryPath;
// the name is written like this, so that no one can easily manipulate.
private String istbamefi;
private static Logger logger = Logger.getLogger(FileHandlingAction.class);
public void prepare() throws Exception {
directoryPath = getText("attachments.dirctory")+File.separator+getS3IdFromSession();
System.out.println("directoryPath::"+directoryPath);
}
@Override
public String execute() throws Exception {
try{
File[] filesStored = Utility.listFilesInDirectory(directoryPath);
if(filesStored==null){
return SUCCESS;
}
for(File fil : filesStored){
attachmentsList.add(fil.getName());
}
}catch(Exception e1){
logger.error(e1.getMessage(),e1);
}
return SUCCESS;
}
public String saveFiles() {
logger.info("Going to save the Uploaded files");
if(logger.isDebugEnabled()){
logger.debug("Directory path :"+directoryPath);
if(file==null){
addActionMessage("Please Select a file to upload");
return INPUT;
}
for (File f : file) {
logger.debug("File :"+f);
}
}
for (int i = 0; i < file.length; i++) {
try {
uploadDocument(file[i], filename[i]);
} catch (IOException e) {
e.printStackTrace();
}
}
logger.info("Exiting successfully after saving the files:");
setMessage("succesfully saved the files");
return SUCCESS;
}
public String removeFile(){
logger.debug("Going to delete file:"+istbamefi);
File file = new File(directoryPath+File.separator+istbamefi);
if(file.exists()){
logger.debug("File found");
file.delete();
logger.debug("File Deleted");
}else{
logger.debug("File Doest not exists");
setMessage("File Does not exists");
return SUCCESS;
}
setMessage("File Successfully Removed");
return SUCCESS;
}
private void uploadDocument(File file, String fileName) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
File dir = new File(directoryPath);
if (!dir.exists()) {
dir.mkdir();
}
logger.info("Uploaded File Name :" +fileName);
logger.info("Uploaded File Length :" +file.length());
String targetPath = dir.getPath() + File.separator + fileName;
System.out.println("targetPath::"+targetPath);
File picDestination = new File(targetPath);
logger.info("Destination Path :"+picDestination);
try {
in = new FileInputStream(file);
out = new FileOutputStream(picDestination);
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
public List<String> getAttachmentsList() {
return attachmentsList;
}
public void setAttachmentsList(List<String> attachmentsList) {
this.attachmentsList = attachmentsList;
}
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
public String[] getContentType() {
return contentType;
}
public void setContentType(String[] contentType) {
this.contentType = contentType;
}
public String[] getFilename() {
return filename;
}
public void setUpload(File[] file) {
this.file = file;
}
public void setUploadContentType(String[] contentType) {
this.contentType = contentType;
}
public void setUploadFileName(String[] filename) {
this.filename = filename;
}
public void setFilename(String[] filename) {
this.filename = filename;
}
public String getDirectoryPath() {
return directoryPath;
}
public void setDirectoryPath(String directoryPath) {
this.directoryPath = directoryPath;
}
public String getIstbamefi() {
return istbamefi;
}
public void setIstbamefi(String istbamefi) {
this.istbamefi = istbamefi;
}
}
公共类FileHandlingAction扩展了BaseAction实现了Preparable{
/**
*
*/
私有静态最终长serialVersionUID=-470622101009022548L;
私有文件[]文件;
私有字符串[]contentType;
私有字符串[]文件名;
私有列表attachmentsList=new ArrayList();
私有字符串目录路径;
//名字是这样写的,所以没有人可以轻易地操纵。
私有字符串istbamefi;
私有静态记录器=Logger.getLogger(FileHandlingAction.class);
public void prepare()引发异常{
directoryPath=getText(“attachments.directory”)+File.separator+getS3IdFromSession();
System.out.println(“directoryPath::”+directoryPath);
}
@凌驾
公共字符串execute()引发异常{
试一试{
File[]filestored=Utility.listFilesInDirectory(directoryPath);
if(filestored==null){
回归成功;
}
for(文件fil:filestored){
attachmentsList.add(fil.getName());
}
}捕获(异常e1){
logger.error(e1.getMessage(),e1);
}
回归成功;
}
公共字符串saveFiles(){
logger.info(“将保存上传的文件”);
if(logger.isDebugEnabled()){
debug(“目录路径:”+directoryPath);
if(file==null){
addActionMessage(“请选择要上载的文件”);
返回输入;
}
for(文件f:File){
调试(“文件:”+f);
}
}
for(int i=0;i
您能检查一下PDF是否有一些内容,或者是否为空(0Kb)?无论哪种情况,发布您的codeYa PDF文件都有内容。它是4 MB,您的操作类FileHandlingAction
是什么样子的?对于上面代码中的FileHandlingAction,不要自己读/写文件。使用类似于FileUtils.copyFile
的内容。