1.JSP里面 zengjia.jsp
上传音乐:<s:form action="upload" method="post" enctype="multipart/form-data">
<s:file name="uploadFile"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
2.struts.xml
- XML code
<?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> <package name="struts3.1" extends="struts-default"> <action name="upload" class="controller.Upload"> <result name="done">/zengjia.jsp</result> <result name="input">/register.jsp</result> </action> </package> <constant name="struts.ui.theme" value="simple"></constant></struts>
3.Upload.java
- Java code
package controller;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class Upload extends ActionSupport { private String username; private File uploadFile; private String uploadFileFileName; @Override public void validate() { } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public File getUploadFile() { return uploadFile; } public void setUploadFile(File uploadFile) { this.uploadFile = uploadFile; } public String getUploadFileFileName() { return uploadFileFileName; } public void setUploadFileFileName(String uploadFileFileName) { this.uploadFileFileName = uploadFileFileName; } public String execute() throws IOException { //用户名 System.out.println("username" + username); //放在哪 String targetDirectory = ServletActionContext.getRequest().getRealPath("/upload"); //生成对象 File target = new File(targetDirectory, uploadFileFileName); //复制对象 FileUtils.copyFile(uploadFile, target); return "done"; }}
具体是这样的,在JSP里面,浏览本地音乐文件,点上传,文件上传到服务器了。
问题来了,我还想做一个下载文件的,上传的那个文件仅归属于当前登录用户。
我如何在"done"指向的那个JSP中得到文件保存在服务器的位置。之所以要得到这个保存的路径是因为,我想在数据库中保存这个地址。用户要下载的时候,我直接从数据库查询出来,做成一个链接。
------解决方案--------------------
你的uploadFileFileName就是文件在服务器的路径,这个参数应该有get/set方法了,在done对应的也没应该可以取到,不清楚你想实现啥功能