juqeryファイルアップロードplupload java修正版
3844 ワード
最近jqueryファイルにプラグインをアップロードする必要があります.pluploadというものがいいことに気づきました.バックグラウンドコードはphpです.tomcatはphpを配置しなければ走りません.そこで少し研究してjavaコードに変更しました.
pluploadの特徴は
1、chunkを構成することができて、1つの大きいファイルを多くの小さいファイルに分けてアップロードして、バックグラウンドはphpを通じて大きいファイルに合併して、ここで相応のjavaコードを通じて
2、実際にアップロードしたファイル名は、生成された唯一のuuidを経て、パラメータnameを通じてバックグラウンドに渡される
3、ファイルをアップロードするプロセスは、uuidと名付けられた一時ファイルをアップロードし、アップロードに成功すると自動的にいくつかのinputラベルが生成され、アップロード後の一時ファイルのファイル名に対応し、その後、別のaction呼び出しuploadFinishで一時ファイルの名前変更操作またはその他の操作を行う
このjavaコードはStruts 2に基づいているのでservletではなく、どうせ似たようなものなので、その上で変更しやすいです
補足:
プロジェクトからファイルにアップロードされたコードを抽出し、単独のeclipseで走れる例をアップロードしました(build pathのjreパスに注意)
オリジナル、転載を歓迎して、源を明記してください
http://asyty.iteye.com/blog/1230119/
pluploadの特徴は
1、chunkを構成することができて、1つの大きいファイルを多くの小さいファイルに分けてアップロードして、バックグラウンドはphpを通じて大きいファイルに合併して、ここで相応のjavaコードを通じて
2、実際にアップロードしたファイル名は、生成された唯一のuuidを経て、パラメータnameを通じてバックグラウンドに渡される
3、ファイルをアップロードするプロセスは、uuidと名付けられた一時ファイルをアップロードし、アップロードに成功すると自動的にいくつかのinputラベルが生成され、アップロード後の一時ファイルのファイル名に対応し、その後、別のaction呼び出しuploadFinishで一時ファイルの名前変更操作またはその他の操作を行う
このjavaコードはStruts 2に基づいているのでservletではなく、どうせ似たようなものなので、その上で変更しやすいです
public class UploadAction extends ActionSupport {
private static final int BUFFER_SIZE = 2 * 1024;
private File upload;
private String name; //plupload uuid.
private String uploadFileName;
private String uploadContentType;
private int chunk;
private int chunks;
// 。。。 getter setter
private void copy(File src, File dst) {
InputStream in = null;
OutputStream out = null;
try {
if (dst.exists()) {
out = new BufferedOutputStream(new FileOutputStream(dst, true),
BUFFER_SIZE); //plupload chunk appand
} else {
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
}
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public String upload() throws Exception {
String dstPath = ServletActionContext.getServletContext().getRealPath("\\tmp")
+ "\\" + this.getName(); //
File dstFile = new File(dstPath);
// ( )
if (chunk == 0 && dstFile.exists()) {
dstFile.delete();
dstFile = new File(dstPath);
}
copy(this.upload, dstFile);
//System.out.println(uploadFileName + " " + uploadContentType + " "
+ chunk + " " + chunks);
if (chunk == chunks - 1) {
//
}
return SUCCESS;
}
public String uploadFinish() {
String dstPath = ServletActionContext.getServletContext().getRealPath("\\tmp");
HttpServletRequest request = ServletActionContext.getRequest();
int count = Integer.parseInt(request.getParameter("uploader_count"));
for (int i = 0; i < count; i++) {
uploadFileName = request.getParameter("uploader_" + i + "_name");
name = request.getParameter("uploader_" + i + "_tmpname");
System.out.println(uploadFileName + " " + name);
try {
//
} catch(Exception e) {
}
}
return SUCCESS;
}
}
補足:
プロジェクトからファイルにアップロードされたコードを抽出し、単独のeclipseで走れる例をアップロードしました(build pathのjreパスに注意)
オリジナル、転載を歓迎して、源を明記してください
http://asyty.iteye.com/blog/1230119/