2018-04-25 18页 doc 39KB 11阅读
is_729658
暂无简介
import java.io.File; 169. 170. import java.io.FileInputStream; 171. 172. import java.io.FileOutputStream; 173. 174. 175. 176. import org.apache.struts2.ServletActionContext; 177. 178. 179. 180. import net.ask123.ecommerce.common.util.Common; 181. 182. import net.ask123.ecommerce.common.util.Constants; 183. 184. import net.ask123.ecommerce.common.util.FileOperate; 185. import net.ask123.ecommerce.common.util.VeDate; 186. 187. 188. 189. 190. /** 191. 192. * 上传的公共方法 193. 194. * 195. 196. * @author sam.zhang 197. 198. * 199. 200. */ 201. 202. public class UploadAction extends ExtJsonActionSuport { 203. 204. 205. 206. /** 207. 208. * 209. 210. */ 211. 212. private static final long serialVersionUID = 1L; 213. 214. 215. //和前台的名字一样,这里约定是 upload 216. private File upload; 217. 218. 219. 220. private String uploadContentType; 221. 222. 223. 224. private String uploadFileName; 225. 226. 227. 228. private String savePath; 229. 230. 231. 232. // 存放允许上传的后缀名 233. 234. private String types; 235. 236. 237. 238. public String getSavePath() { 239. 240. return savePath; 241. 242. } 243. 244. 245. 246. public void setSavePath(String savePath) { 247. 248. this.savePath = savePath; 249. 250. } 251. 252. 253. 254. public File getUpload() { 255. 256. return upload; 257. 258. } 259. 260. 261. 262. public void setUpload(File upload) { 263. 264. this.upload = upload; 265. 266. } 267. 268. 269. 270. public String getUploadContentType() { 271. 272. return uploadContentType; 273. } 274. 275. 276. 277. 278. public void setUploadContentType(String uploadContentT ype) { 279. 280. this.uploadContentType = uploadContentType; 281. 282. } 283. 284. 285. 286. public String getUploadFileName() { 287. 288. return uploadFileName; 289. 290. } 291. 292. 293. 294. public void setUploadFileName(String uploadFileName) { 295. 296. this.uploadFileName = uploadFileName; 297. 298. } 299. 300. 301. 302. public String getTypes() { 303. 304. return types; 305. 306. } 307. 308. 309. 310. public void setTypes(String types) { 311. 312. this.types = types; 313. 314. } 315. 316. 317. 318. @SuppressWarnings("deprecation") 319. 320. public String execute() throws Exception { 321. 322. String msg = ""; 323. 324. FileOperate fo = new FileOperate(); 325. 326. 327. 328. String sid = VeDate.getNo(4); 329. 330. 331. 332. this.savePath = "/updownFiles"; 333. 334. 335. 336. try { 337. 338. 339. 340. // ...获取文件后缀名 341. 342. String ext = fo.getFileExt(getUploadFileName() ); 343. 344. 345. 346. if ("".equals(this.types) 347. 348. || Common.indexofString(this.types, "- ") == -1) { 349. 350. msg = "上传失败"; 351. 352. this.setJsonString("{success:false,msg:'" + msg + "'}"); 353. return SUCCESS; 354. 355. 356. } 357. 358. 359. 360. // ...判断上传的文件是否合法 361. 362. boolean istrue = FileOperate.trueExt(this.type s.split("-"), ext); 363. 364. if (!istrue) { 365. 366. msg = "您上传的文件格式不正确,正确格式为 " + this.types; 367. 368. this.setJsonString("{success:false,msg:'" + msg + "'}"); 369. 370. return SUCCESS; 371. 372. } 373. 374. 375. 376. // ...文件存放的位置 377. 378. String sPath = ServletActionContext.getRequest ().getRealPath( 379. 380. this.getSavePath()) 381. 382. + Constants.FILESPARA 383. 384. + sid.substring(0, 4) 385. 386. + Constants.FILESPARA 387. 388. + sid.substring(4, 6) 389. 390. + Constants.FILESPARA; 391. 392. 393. // ...保存在数据库的路径 394. 395. 396. String filePath = this.savePath + "/" + sid.su + "/" bstring(0, 4) 397. 398. + sid.substring(4, 6) + "/" + sid + ". " + ext; 399. 400. 401. 402. // 如果目录不存在则创建它 403. 404. 405. 406. fo.createFolder(sPath); 407. 408. 409. 410. FileOutputStream fileOutputStream = new FileOu tputStream(sPath 411. 412. + sid + "." + ext); 413. 414. 415. 416. FileInputStream fileInputStream = new FileInpu tStream(getUpload()); 417. 418. // ... 419. 420. byte[] buffer = new byte[1024]; 421. 422. int len = 0; 423. 424. while ((len = fileInputStream.read(buffer)) > 0) { 425. 426. fileOutputStream.write(buffer, 0, len); 427. 428. } 429. 430. this.setJsonString("{success:true,msg:'" + fil ePath + "'}"); 431. } catch (Exception e) { 432. 433. 434. this.setJsonString("{success:false}"); 435. 436. e.printStackTrace(); 437. 438. } 439. 440. return SUCCESS; 441. 442. } 443. 444. 445. 446. } 447. 448.449. 这里就大致贴贴一下吧,如果看不懂的话把struts2学习下就ok了, 这里不一定后台一定是java的, 关键是 Jscript代码 1. url:'uploadAction.action?types='+types,这里会指定后台的请求地 址