- 爱易网页
-
Java教程
- 求解struts2上传时文件接不到有关问题
日期:2014-05-18 浏览次数:20765 次
求解struts2上传时文件接不到问题。
JSP
<form action="uploadMusicAction!upload.action" enctype="multipart/form-data">
<div class="size">标题:<input type="text" name="title" class="uploadMusic"/></div>
<div> </div>
<div class="size">文件:<s:file name="upload" cssClass="uploadMusic"/></div>
<div> </div>
<div class="size2"><input type="submit" value="上传" class="uploadButton"></div>
</form>
struts.xml
[color=#00CCFF] <action name="uploadMusicAction" class="uploadMusicSpring">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg
</param>
<!-- 配置允许上传的文件大小,单位字节 -->
<param name="maximumSize">1024000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<result name="uploadSuccess">/music/uploadMusic.jsp</result>
</action>
Java类
public class UploadMusicAction {
private String message;
private IUploadMusicService uploadMusicService;
private File upload;
private String uploadFileName;
private String uploadContentType;
private String title;
public String upload() throws IOException {
System.out.println("upload======"+upload);
System.out.println("title======"+title);
String path = ServletActionContext.getServletContext().getRealPath("/upload");
System.out.println(path);
if(upload != null){
File savefile = new File(new File(path),uploadFileName);
if(!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
try {
FileUtils.copyFile(upload , savefile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] t
免责声明: 本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。