我有Web项目我正在使用Struts2框架。在这个人上传pdf文件,因为我使用了struts文件上传API。
但奇怪的是,每次第二次文件上传都在服务器上损坏,当我尝试打开文件时,它给了我错误:
打开此文档时出错。该文件已损坏且无法修复。 enter image description here
struts.xml中
<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>
FileHandlingAction:
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;
}
}