纠结了一天,怎么也改不了路径,求大神帮忙!!
Java代码:
package com.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.net.httpserver.Authenticator.Success;
public class FileUpLodAction extends ActionSupport{
private final static String uplod_url = "/Affile";
private List<File> file;
private List<String> fileFileName;
private List<String> fileFileContentType;
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> getFileFileContentType() {
return fileFileContentType;
}
public void setFileFileContentType(List<String> fileFileContentType) {
this.fileFileContentType = fileFileContentType;
}
public static String getUplodUrl() {
return uplod_url;
}
public void getUplod(int i) throws Exception{
InputStream is = new FileInputStream(file.get(i));
String root = ServletActionContext.getServletContext().getRealPath(uplod_url);
//上传的文件
File uploadFile = new File(root, this.getFileFileName().get(i));
OutputStream os= new FileOutputStream(uploadFile);
byte [] buffer = new byte[1024*1024];
int length;
while((length = is.read(buffer))> 0){
os.write(buffer,0,length);
}
if(is!=null)
is.close();
if(os!=null)
os.close();
}
public String uplod(){
for(int i=0;i<file.size();i++){
try {
this.getUplod(i);
} catch (Exception e) {
e.printStackTrace();
}
}
return "success";
}
}
Struts2配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<!-- 文件上传 -->
<!--index页面有跳转成功的提示-->
/index.jsp
jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
/s:file
/s:submit
/s:form