书签 分享 收藏 举报 版权申诉 / 20

类型struts2文件上传.docx

  • 文档编号:11057363
  • 上传时间:2023-02-24
  • 格式:DOCX
  • 页数:20
  • 大小:18.74KB
  

22.  

23.    

24.  

25.   

26.  

27.2.访问到的struts.xml文件  

28.  

29.

xml version="1.0" encoding="GBK"?

>  

30.  

31.

DOCTYPE struts PUBLIC  

32.  

33.    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  

34.  

35.    "http:

//struts.apache.org/dtds/struts-2.3.dtd">  

36.  

37.  

38.  

39.      

40.  

41.         

42.  

43.           ../success.jsp  

44.  

45.         

46.  

47.      

48.  

49.  

50.  

51.3.访问到UploadAction.java的action    

52.  

53.package cn.csdn.hr.up.action;  

54.  

55.   

56.  

57.import java.io.File;  

58.  

59.import java.io.IOException;  

60.  

61.   

62.  

63.import mons.io.FileUtils;  

64.  

65.import org.apache.struts2.ServletActionContext;  

66.  

67.   

68.  

69.import com.opensymphony.xwork2.ActionSupport;  

70.  

71.   

72.  

73.public class UploadAction extends ActionSupport {  

74.  

75.    private static final long serialVersionUID = 1L;  

76.  

77.   

78.  

79.    /** 

80. 

81.     都是规定,File的对象和input中的name(xxx)相同,文件类型和文件名都是xxxContextType,xxxFileName 

82. 

83.     */  

84.  

85.   

86.  

87.    // 得到上传文件的名称一定与name值一直  

88.  

89.    private File upload;  

90.  

91.    // 上传文件的类型 ContentType  

92.  

93.    private String uploadContentType;  

94.  

95.    // 上传文件的名称  

96.  

97.    private String uploadFileName;  

98.  

99.   

100.  

101.    public File getUpload() {  

102.  

103.       return upload;  

104.  

105.    }  

106.  

107.   

108.  

109.    public void setUpload(File upload) {  

110.  

111.       this.upload = upload;  

112.  

113.    }  

114.  

115.   

116.  

117.    public String getUploadContentType() {  

118.  

119.       return uploadContentType;  

120.  

121.    }  

122.  

123.   

124.  

125.    public void setUploadContentType(String uploadContentType) {  

126.  

127.       this.uploadContentType = uploadContentType;  

128.  

129.    }  

130.  

131.   

132.  

133.    public String getUploadFileName() {  

134.  

135.       return uploadFileName;  

136.  

137.    }  

138.  

139.   

140.  

141.    public void setUploadFileName(String uploadFileName) {  

142.  

143.       this.uploadFileName = uploadFileName;  

144.  

145.    }  

146.  

147.   

148.  

149.    public static long getSerialversionuid() {  

150.  

151.       return serialVersionUID;  

152.  

153.    }  

154.  

155.   

156.  

157.    public String upload() {  

158.  

159.       String path = ServletActionContext.getServletContext().getRealPath(  

160.  

161.              "/upload");  

162.  

163.       // 写到指定路径  

164.  

165.       File file = new File(path);  

166.  

167.       //判断指定的路径下是否有uplaod,如果没有,自动创建  

168.  

169.       if (!

file.exists()) {  

170.  

171.           file.mkdirs();  

172.  

173.       }  

174.  

175.       try {  

176.  

177.           FileUtils.copyFile(upload, new File(file, uploadFileName));  

178.  

179.       } catch (IOException e) {  

180.  

181.           // TODO Auto-generated catch block  

182.  

183.           e.printStackTrace();  

184.  

185.       }  

186.  

187.       return SUCCESS;  

188.  

189.    }  

190.  

191.}  

192.  

193.二  多个文件的上传  

194.  

195.多个文件的上传不同的是在action中的获取到的是数组或者是list集合  

196.  

197.   

198.  

199.数组的action为:

  

200.  

201.package cn.csdn.hr.up.action;  

202.  

203.   

204.  

205.import java.io.File;  

206.  

207.import java.io.IOException;  

208.  

209.   

210.  

211.import mons.io.FileUtils;  

212.  

213.import org.apache.struts2.ServletActionContext;  

214.  

215.   

216.  

217.import com.opensymphony.xwork2.ActionSupport;  

218.  

219.   

220.  

221.public class UploadsAction extends ActionSupport {  

222.  

223.    private static final long serialVersionUID = 1L;  

224.  

225.   

226.  

227.    // 得到上传文件的名称一定与name值一直  

228.  

229.    private File upload[];  

230.  

231.    // 上传文件的类型 ContentType  

232.  

233.    private String uploadContentType[];  

234.  

235.    // 上传文件的名称  

236.  

237.    private String uploadFileName[];  

238.  

239.   

240.  

241.    public File[] getUpload() {  

242.  

243.       return upload;  

244.  

245.    }  

246.  

247.   

248.  

249.    public void setUpload(File[] upload) {  

250.  

251.       this.upload = upload;  

252.  

253.    }  

254.  

255.   

256.  

257.    public String[] getUploadContentType() {  

258.  

259.       return uploadContentType;  

260.  

261.    }  

262.  

263.   

264.  

265.    public void setUploadContentType(String[] uploadContentType) {  

266.  

267.       this.uploadContentType = uploadContentType;  

268.  

269.    }  

270.  

271.   

272.  

273.    public String[] getUploadFileName() {  

274.  

275.       return uploadFileName;  

276.  

277.    }  

278.  

279.   

280.  

281.    public void setUploadFileName(String[] uploadFileName) {  

282.  

283.       this.uploadFileName = uploadFileName;  

284.  

285.    }  

286.  

287.   

288.  

289.    public static long getSerialversionuid() {  

290.  

291.       return serialVersionUID;  

292.  

293.    }  

294.  

295.   

296.  

297.    public String uploads() {  

298.  

299.       String path = ServletActionContext.getServletContext().getRealPath(  

300.  

301.              "/upload");  

302.  

303.   

304.  

305.       // 写到指定路径  

306.  

307.       File file = new File(path);  

308.  

309.       //判断指定的路径下是否有uplaod,如果没有,自动创建  

310.  

311.       if (!

file.exists()) {  

312.  

313.           file.mkdirs();  

314.  

315.       }  

316.  

317.       try {  

318.  

319.           for(int i = 0;i

320.  

321.              FileUtils.copyFile(upload[i], new File(file, uploadFileName[i]));  

322.  

323.           }  

324.  

325.       } catch (IOException e) {  

326.  

327.           // TODO Auto-generated catch block  

328.  

329.           e.printStackTrace();  

330.  

331.       }  

332.  

333.       System.out.println("上传文件的名称:

" + uploadFileName + "上传的路径:

" + path  

334.  

335.              + "上传的类型:

" + uploadContentType);  

336.  

337.       return SUCCESS;  

338.  

339.    }  

340.  

341.}  

342.  

343.   

344.  

345.   

346.  

347.List集合的action为:

;  

348.  

349.   

350.  

351.   

352.  

353.   

354.  

355.package cn.csdn.hr.up.action;  

356.  

357.   

358.  

359.import java.io.File;  

360.  

361.import java.io.IOException;  

362.  

363.import java.util.List;  

364.  

365.   

366.  

367.import mons.io.FileUtils;  

368.  

369.import org.apache.struts2.ServletActionContext;  

370.  

371.   

372.  

373.import com.opensymphony.xwork2.ActionSupport;  

374.  

375.   

376.  

377.public class UploadListAction extends ActionSupport {  

378.  

379.         private static final long serialVersionUID = 1L;  

380.  

381.         // 得到上传文件的名称一定与name值一直  

382.  

383.         private List upload;  

384.  

385.         // 上传文件的类型 ContentType  

386.  

387.         private List uploadContentType;  

388.  

389.         // 上传文件的名称  

390.  

391.         private List uploadFileName;  

392.  

393.   

394.  

395.         public List getUpload() {  

396.  

397.                   return upload;  

398.  

399.         }  

400.  

401.   

402.  

403.         public void setUpload(List upload) {  

404.  

405.                   this.upload = upload;  

406.  

407.         }  

408.  

409.   

410.  

411.         public List getUploadContentType() {  

412.  

413.                   return uploadContentType;  

414.  

415.         }  

416.  

417.   

418.  

419.         public void setUploadContentType(List uploadContentType) {  

420.  

421.                   this.uploadContentType = uploadContentType;  

422.  

423.         }  

424.  

425.   

426.  

427.         public List getUploadFileName() {  

428.  

429.                   return uploadFileName;  

430.  

431.         }  

432.  

433.   

434.  

435.         public void setUploadFileName(List uploadFileName) {  

436.  

437.                   this.uploadFileName = uploadFileName;  

438.  

439.         }  

440.  

441.   

442.  

443.         public static long getSerialversionuid() {  

444.  

445.                   return serialVersionUID;  

446.  

447.         }  

448.  

449.   

450.  

451.         public String uploadList() {  

452.  

453.                   String path = ServletActionContext.getServletContext().getRealPath(  

454.  

455.                                     "/upload");  

456.  

457.   

458.  

459.                   // 写到指定路径  

460.  

461.                   File file = new File(path);  

462

配套讲稿:

如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

特殊限制:

部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

关 键  词:
struts2 文件 上传
提示  冰豆网所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
关于本文
本文标题:struts2文件上传.docx
链接地址:https://www.bdocx.com/doc/11057363.html
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2008-2022 冰点文档网站版权所有

经营许可证编号:鄂ICP备2022015515号-1

收起
展开